{"record":{"id":"f4cc162a8f025854","repo":"karatelabs/karate","slug":"tofixed-digits-argument-must-be-between-0-and-100","errorCode":null,"errorMessage":"toFixed() digits argument must be between 0 and 100","messagePattern":"toFixed\\(\\) digits argument must be between 0 and 100","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsScalars.java","lineNumber":59,"sourceCode":"\n    /**\n     * Spec §21.3.2.28: NaN/±Inf/±0/integer unchanged; (0, 0.5) -&gt; +0; [-0.5, 0) -&gt; -0;\n     * otherwise floor(x + 0.5). Note this is \"round half toward +Infinity\", NOT \"round half away\n     * from zero\": Math.round(-1.5) === -1, NOT -2. The integer short-circuit is load-bearing near\n     * MAX_SAFE_INTEGER (ulp &ge; 1), where x + 0.5 rounds to a different integer than x.\n     */\n    public static double round(double x) {\n        if (Double.isNaN(x) || Double.isInfinite(x) || x == 0) return x;\n        if (x == Math.floor(x)) return x;\n        if (x > 0 && x < 0.5) return 0.0;\n        if (x < 0 && x >= -0.5) return -0.0;\n        return Math.floor(x + 0.5);\n    }\n\n    /** Spec §21.1.3.3, with the same RangeError text the installed method throws. */\n    public static String toFixed(double value, int digits) {\n        if (digits < 0 || digits > 100) {\n            throw JsErrorException.rangeError(\"toFixed() digits argument must be between 0 and 100\");\n        }\n        if (Double.isNaN(value)) return \"NaN\";\n        if (value == Double.POSITIVE_INFINITY) return \"Infinity\";\n        if (value == Double.NEGATIVE_INFINITY) return \"-Infinity\";\n        // Spec: |x| ≥ 10^21 falls back to ToString(x); BigDecimal of such doubles\n        // produces a noisy decimal expansion (e.g. 1e21 -> \"1000000000000000040000\")\n        // that doesn't match JS's \"1e+21\" canonical form.\n        if (Math.abs(value) >= 1e21) {\n            return Terms.numberToString(value);\n        }\n        BigDecimal bd = BigDecimal.valueOf(value);\n        bd = bd.setScale(digits, RoundingMode.HALF_UP);\n        StringBuilder pattern = new StringBuilder(\"0\");\n        if (digits > 0) {\n            pattern.append(\".\");\n            pattern.append(\"0\".repeat(digits));\n        }\n        DecimalFormat df = new DecimalFormat(pattern.toString());","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsScalars.java#L41-L77","documentation":"toFixed() was called with a digits argument outside the allowed 0-100 range. Per ECMAScript §21.1.3.3 the fraction-digit count must be an integer between 0 and 100 inclusive; the engine mirrors the spec RangeError text.","triggerScenarios":"Number.prototype.toFixed(n) where n < 0 or n > 100, e.g. (1.5).toFixed(-1) or (1.5).toFixed(101).","commonSituations":"Computing precision from a variable or config that went negative or unbounded; a typo like toFixed(1000); user-supplied precision values not clamped before use.","solutions":["Clamp the digits argument: Math.min(100, Math.max(0, digits)).","Check the source of the precision value for off-by-one or sign errors.","Use Math.round/truncation manually if you intentionally need more than 100 digits."],"exampleFix":"// before\nvalue.toFixed(digits)\n// after\nvalue.toFixed(Math.min(100, Math.max(0, digits)))","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(digits) || digits < 0 || digits > 100) throw new Error('digits must be 0-100');","typeGuard":null,"tryCatchPattern":"try { return value.toFixed(digits); } catch (e) { if (String(e).includes('toFixed() digits')) return value.toFixed(2); throw e; }","preventionTips":["Clamp user/config-supplied precision to 0-100.","Never pass negative or computed-unbounded digit counts.","Add a guard helper: safeToFixed(v, d) { return v.toFixed(Math.min(100, Math.max(0, d))); }"],"tags":["javascript","number","range-error"],"backgroundTag":"value-out-of-range","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}