{"record":{"id":"0e00304f53a15b87","repo":"Blankj/AndroidUtilCode","slug":"precision-shouldn-t-be-less-than-zero","errorCode":null,"errorMessage":"precision shouldn't be less than zero!","messagePattern":"precision shouldn't be less than zero!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/ConvertUtils.java","lineNumber":470,"sourceCode":"     * @return fit size of memory\n     */\n    @SuppressLint(\"DefaultLocale\")\n    public static String byte2FitMemorySize(final long byteSize) {\n        return byte2FitMemorySize(byteSize, 3);\n    }\n\n    /**\n     * Size of byte to fit size of memory.\n     * <p>to three decimal places</p>\n     *\n     * @param byteSize  Size of byte.\n     * @param precision The precision\n     * @return fit size of memory\n     */\n    @SuppressLint(\"DefaultLocale\")\n    public static String byte2FitMemorySize(final long byteSize, int precision) {\n        if (precision < 0) {\n            throw new IllegalArgumentException(\"precision shouldn't be less than zero!\");\n        }\n        if (byteSize < 0) {\n            throw new IllegalArgumentException(\"byteSize shouldn't be less than zero!\");\n        } else if (byteSize < MemoryConstants.KB) {\n            return String.format(\"%.\" + precision + \"fB\", (double) byteSize);\n        } else if (byteSize < MemoryConstants.MB) {\n            return String.format(\"%.\" + precision + \"fKB\", (double) byteSize / MemoryConstants.KB);\n        } else if (byteSize < MemoryConstants.GB) {\n            return String.format(\"%.\" + precision + \"fMB\", (double) byteSize / MemoryConstants.MB);\n        } else {\n            return String.format(\"%.\" + precision + \"fGB\", (double) byteSize / MemoryConstants.GB);\n        }\n    }\n\n    /**\n     * Time span in unit to milliseconds.\n     *\n     * @param timeSpan The time span.","sourceCodeStart":452,"sourceCodeEnd":488,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/ConvertUtils.java#L452-L488","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nString s = ConvertUtils.byte2FitMemorySize(fileLen, precisionFromConfig); // precisionFromConfig == -1\n\n// after\nint precision = precisionFromConfig < 0 ? 2 : precisionFromConfig;\nString s = ConvertUtils.byte2FitMemorySize(fileLen, precision);","handlingStrategy":"validation","validationCode":"// Validate precision before formatting\nint precision = precisionFromConfig;\nif (precision < 0) {\n    precision = 2; // or 0 for no decimals\n}\nString s = ConvertUtils.byte2FitMemorySize(byteSize, precision);","typeGuard":"// Coerce/validate the precision integer before the call\npublic static int safePrecision(int p) {\n    return p < 0 ? 2 : p;\n}","tryCatchPattern":"try {\n    String s = ConvertUtils.byte2FitMemorySize(byteSize, precision);\n} catch (IllegalArgumentException e) {\n    // precision (or byteSize) was negative; clamp and retry once\n    String s = ConvertUtils.byte2FitMemorySize(Math.max(0, byteSize), Math.max(0, precision));\n}","preventionTips":["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."],"tags":["validation","convert","formatting","argument-check"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}