Merge "FormatUtil: Fix Math#round() truncation error flagged by error-prone" into stable-2.14
This commit is contained in:
commit
7fea932270
@ -137,7 +137,25 @@ public class FormatUtil {
|
||||
if (size == 0) {
|
||||
return Resources.C.notAvailable();
|
||||
}
|
||||
int p = Math.abs(Math.round(delta * 100 / size));
|
||||
int p = Math.abs(saturatedCast(delta * 100 / size));
|
||||
return p + "%";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@code int} nearest in value to {@code value}.
|
||||
*
|
||||
* @param value any {@code long} value
|
||||
* @return the same value cast to {@code int} if it is in the range of the {@code int} type,
|
||||
* {@link Integer#MAX_VALUE} if it is too large, or {@link Integer#MIN_VALUE} if it is too
|
||||
* small
|
||||
*/
|
||||
private static int saturatedCast(long value) {
|
||||
if (value > Integer.MAX_VALUE) {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
if (value < Integer.MIN_VALUE) {
|
||||
return Integer.MIN_VALUE;
|
||||
}
|
||||
return (int) value;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user