{"record":{"id":"cdeee500f2f0fe5b","repo":"didi/DoKit","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":"Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ConvertUtils.java","lineNumber":471,"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":453,"sourceCodeEnd":489,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ConvertUtils.java#L453-L489","documentation":"ConvertUtils.byte2FitMemorySize(long byteSize, int precision) formats a byte count into a human-readable B/KB/MB/GB string and requires precision >= 0 because precision is interpolated directly into a '%.<precision>f' format string. A negative value would produce an invalid format specifier, so it is rejected up front with IllegalArgumentException.","triggerScenarios":"Passing a negative precision, most commonly a constant or parsed value: byte2FitMemorySize(bytes, -1); reading precision from config/user input without validation; using -1 as a 'default' sentinel.","commonSituations":"Config files or intent extras supplying -1 to mean 'unspecified'; copying the 3-precision signature but defaulting the parameter to -1; arithmetic like precision = requested - actual that can dip below zero.","solutions":["Pass a valid precision; the common library default is 3.","Sanitize external input: precision = Math.max(0, precision) or reject with your own validation error.","Use the single-argument overload byte2FitMemorySize(byteSize) which internally uses a fixed valid precision."],"exampleFix":"// before\nString s = ConvertUtils.byte2FitMemorySize(Runtime.getRuntime().totalMemory(), -1); // throws\n\n// after\nString s = ConvertUtils.byte2FitMemorySize(Runtime.getRuntime().totalMemory(), 3);","handlingStrategy":"validation","validationCode":"int safePrecision = Math.max(0, precision);\nString s = ConvertUtils.byte2FitMemorySize(byteSize, safePrecision);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat precision as a validated config value, not an arbitrary integer.","Use the single-arg overload when you just want the default formatting."],"tags":["formatting","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}