{"record":{"id":"3bf730a0a8cbaeb1","repo":"karatelabs/karate","slug":"cannot-convert-a-bigint-to-a-number-jsnumberprototype","errorCode":null,"errorMessage":"Cannot convert a BigInt to a number","messagePattern":"Cannot convert a BigInt to a number","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsNumberPrototype.java","lineNumber":58,"sourceCode":"\n    private JsNumberPrototype() {\n        super(JsObjectPrototype.INSTANCE);\n        install(\"toFixed\", 1, this::toFixed);\n        install(\"toPrecision\", 1, this::toPrecision);\n        install(\"toExponential\", 1, this::toExponential);\n        install(\"toLocaleString\", 0, this::toLocaleString);\n        install(\"toString\", 1, this::toStringMethod);\n        install(\"valueOf\", 0, this::valueOf);\n    }\n\n    private Object toStringMethod(Context context, Object[] args) {\n        Number n = thisNumber(context);\n        // Spec: only `undefined` (or absent) defaults to radix 10; `null` runs through\n        // ToInteger → 0 → RangeError.\n        if (args.length > 0 && args[0] != Terms.UNDEFINED) {\n            // ToIntegerOrInfinity rejects BigInt — spec mandates TypeError.\n            if (args[0] instanceof java.math.BigInteger) {\n                throw JsErrorException.typeError(\"Cannot convert a BigInt to a number\");\n            }\n            int radix = Terms.objectToNumber(args[0]).intValue();\n            if (radix < 2 || radix > 36) {\n                throw JsErrorException.rangeError(\"toString() radix must be between 2 and 36\");\n            }\n            if (radix != 10) {\n                double d = n.doubleValue();\n                if (d == Math.floor(d) && !Double.isInfinite(d)) {\n                    return Long.toString((long) d, radix);\n                }\n                return Double.toString(d);\n            }\n        }\n        return Terms.numberToString(n);\n    }\n\n    /**\n     * Spec {@code thisNumberValue} §21.1.3: unwrap JsNumber, accept primitive","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsNumberPrototype.java#L40-L76","documentation":"`Number.prototype.toString(radix)` runs its radix argument through ToIntegerOrInfinity, which must reject BigInt values per the ES spec. Karate throws this TypeError when a BigInt (java.math.BigInteger) is passed as the radix instead of a regular number.","triggerScenarios":"`(255).toString(bigIntValue)` where the radix came from BigInt arithmetic like `2n ** 4n`; passing a BigInt parsed from JSON or Karate variable interpolation as the radix.","commonSituations":"Mixed BigInt/number arithmetic in tests where the radix was computed with BigInt literals; data-driven tests where a JSON/CSV value became a BigInt.","solutions":["Convert the BigInt to a regular number first: `(255).toString(Number(radix))`","Write BigInt literals without the `n` suffix when you intend a numeric radix","Validate `typeof radix === 'number'` before calling toString"],"exampleFix":"// before\n(255).toString(16n); // TypeError\n// after\n(255).toString(Number(16n));","handlingStrategy":"validation","validationCode":"if (typeof radix !== 'number' || !Number.isInteger(radix)) { throw new Error('radix must be a plain integer number, got ' + typeof radix); }","typeGuard":"function isNumberRadix(r) { return typeof r === 'number' && Number.isInteger(r); }","tryCatchPattern":"try {\n  s = n.toString(radix);\n} catch (e) {\n  if (String(e.message).includes('BigInt')) {\n    s = n.toString(Number(radix));\n  } else throw e;\n}","preventionTips":["Use Number() on any radix that may originate from BigInt arithmetic","Avoid `n` suffixes where a plain number is meant","Type-check radix before calling toString"],"tags":["javascript","number","bigint","typeerror","radix"],"backgroundTag":"type-mismatch","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}