Blankj/AndroidUtilCode · error · IllegalArgumentException
precision shouldn't be less than zero!
Error message
precision shouldn't be less than zero!
What it means
ConvertUtils.byte2FitMemorySize(long, int) formats a byte count into a human-readable B/KB/MB/GB string with a caller-specified number of decimal places. A negative precision is meaningless for String.format("%.Nf...") and would produce a FormatFlagsConversionMismatchException / UnknownFormatConversionException, so the method fails fast with IllegalArgumentException instead.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ConvertUtils.java:470
* @return fit size of memory
*/
@SuppressLint("DefaultLocale")
public static String byte2FitMemorySize(final long byteSize) {
return byte2FitMemorySize(byteSize, 3);
}
/**
* Size of byte to fit size of memory.
* <p>to three decimal places</p>
*
* @param byteSize Size of byte.
* @param precision The precision
* @return fit size of memory
*/
@SuppressLint("DefaultLocale")
public static String byte2FitMemorySize(final long byteSize, int precision) {
if (precision < 0) {
throw new IllegalArgumentException("precision shouldn't be less than zero!");
}
if (byteSize < 0) {
throw new IllegalArgumentException("byteSize shouldn't be less than zero!");
} else if (byteSize < MemoryConstants.KB) {
return String.format("%." + precision + "fB", (double) byteSize);
} else if (byteSize < MemoryConstants.MB) {
return String.format("%." + precision + "fKB", (double) byteSize / MemoryConstants.KB);
} else if (byteSize < MemoryConstants.GB) {
return String.format("%." + precision + "fMB", (double) byteSize / MemoryConstants.MB);
} else {
return String.format("%." + precision + "fGB", (double) byteSize / MemoryConstants.GB);
}
}
/**
* Time span in unit to milliseconds.
*
* @param timeSpan The time span.View on GitHub (pinned to 7b4caf9e54)
Solutions
- Coerce the precision to a non-negative value before calling: use Math.max(0, precision), or default to 0/2 when the source value is negative/unset.
- Treat the -1 sentinel from your config as 'use default precision' and substitute a sensible constant (e.g. 2).
- Validate at the boundary that produced the precision and reject the input there instead of letting it reach byte2FitMemorySize.
- If you truly need 'no decimals', pass precision = 0 rather than a negative number.
Example fix
// before String s = ConvertUtils.byte2FitMemorySize(fileLen, precisionFromConfig); // precisionFromConfig == -1 // after int precision = precisionFromConfig < 0 ? 2 : precisionFromConfig; String s = ConvertUtils.byte2FitMemorySize(fileLen, precision);
Defensive patterns
Strategy: validation
Validate before calling
// Validate precision before formatting
int precision = precisionFromConfig;
if (precision < 0) {
precision = 2; // or 0 for no decimals
}
String s = ConvertUtils.byte2FitMemorySize(byteSize, precision); Type guard
// Coerce/validate the precision integer before the call
public static int safePrecision(int p) {
return p < 0 ? 2 : p;
} Try / catch
try {
String s = ConvertUtils.byte2FitMemorySize(byteSize, precision);
} catch (IllegalArgumentException e) {
// precision (or byteSize) was negative; clamp and retry once
String s = ConvertUtils.byte2FitMemorySize(Math.max(0, byteSize), Math.max(0, precision));
} Prevention
- Map any -1 'unset' sentinel in your config to a concrete default precision (e.g. 2) at the read boundary.
- Use Math.max(0, precision) when the value is externally supplied.
- Keep precision modelled as unsigned in spirit; never let subtraction produce a negative decimals count.
When it happens
Trigger: Calling byte2FitMemorySize(size, precision) with a precision argument that is negative, typically because a configurable format setting defaulted to -1 (meaning 'unset'), or because an unsigned field was modelled as a signed int and underflowed.
Common situations: Reading precision from a config/SharedPreferences where the default sentinel is -1; arithmetic on lengths that goes negative; passing a 'decimals' value from an API that uses -1 to mean 'not specified'; copy-paste from a percentage formatter that allowed negatives.
Related errors
- byteSize shouldn't be less than zero!
- The key is null.
- The duration is less than 0.
- key must be between 1 and 256 bytes
- comparator must not be null
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/0e00304f53a15b87.
Report an issue: GitHub.