{"record":{"id":"617e185fe3b69e9a","repo":"karatelabs/karate","slug":"cannot-convert-non-integer-number-to-bigint","errorCode":null,"errorMessage":"Cannot convert non-integer number to BigInt","messagePattern":"Cannot convert non-integer number to BigInt","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsBigIntConstructor.java","lineNumber":97,"sourceCode":"    // input is guaranteed primitive.\n    static BigInteger toBigInt(Object value) {\n        return toBigInt(value, null);\n    }\n\n    private static BigInteger primitiveToBigInt(Object value) {\n        if (value instanceof BigInteger bi) {\n            return bi;\n        }\n        if (value instanceof Boolean b) {\n            return b ? BigInteger.ONE : BigInteger.ZERO;\n        }\n        if (value instanceof Number n) {\n            double d = n.doubleValue();\n            if (!Double.isFinite(d)) {\n                throw JsErrorException.rangeError(\"Cannot convert non-finite number to BigInt\");\n            }\n            if (d != Math.floor(d)) {\n                throw JsErrorException.rangeError(\"Cannot convert non-integer number to BigInt\");\n            }\n            return new BigDecimal(d).toBigInteger();\n        }\n        if (value instanceof String s) {\n            String trimmed = s.trim();\n            if (trimmed.isEmpty()) {\n                return BigInteger.ZERO;\n            }\n            try {\n                if (trimmed.length() > 2 && trimmed.charAt(0) == '0') {\n                    char c = trimmed.charAt(1);\n                    if (c == 'x' || c == 'X') return new BigInteger(trimmed.substring(2), 16);\n                    if (c == 'o' || c == 'O') return new BigInteger(trimmed.substring(2), 8);\n                    if (c == 'b' || c == 'B') return new BigInteger(trimmed.substring(2), 2);\n                }\n                return new BigInteger(trimmed);\n            } catch (NumberFormatException e) {\n                throw JsErrorException.syntaxError(\"Cannot convert \" + s + \" to a BigInt\");","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsBigIntConstructor.java#L79-L115","documentation":"BigInt() called with a JavaScript number that is finite but not a whole number (or throws a sibling RangeError if non-finite) throws this RangeError, matching the ECMAScript BigInt::NumberToBigInt abstract operation. Karate's embedded JS engine enforces the spec rule that only integral numbers can be converted into an arbitrarily large BigInteger.","triggerScenarios":"BigInt(1.5), BigInt(0.1), BigInt(-2.0001), or BigInt(x) where x comes from arithmetic like 10/3 or a parsed decimal JSON number.","commonSituations":"Converting numeric API response fields (averages, ratios, prices like 19.99) to BigInt; dividing integers in Karate JS before passing to BigInt; using Math results (Math.sqrt(2)) as BigInt input.","solutions":["Round or truncate the number first: BigInt(Math.trunc(1.5))","Verify the value is integral at runtime before converting: if (Number.isInteger(x)) BigInt(x)","If a fraction is expected, keep it as a Number or scale it (BigInt(Math.round(19.99 * 100))) instead","Parse the value from a string without the fractional part: BigInt('19') instead of BigInt(19.0 from string)"],"exampleFix":"// before\nlet bi = BigInt(avgLatency); // avgLatency = 12.5 -> RangeError\n// after\nlet bi = BigInt(Math.trunc(avgLatency)); // or Number.isInteger(avgLatency) ? BigInt(avgLatency) : 0n","handlingStrategy":"validation","validationCode":"function toBigIntSafe(x) {\n  if (typeof x !== 'number') return null;\n  if (!Number.isFinite(x)) return null;\n  if (!Number.isInteger(x)) return null;\n  return BigInt(x);\n}","typeGuard":"const isBigIntable = (x) => typeof x === 'number' && Number.isFinite(x) && Number.isInteger(x);","tryCatchPattern":"let bi;\ntry { bi = BigInt(x); } catch (e) {\n  if (e instanceof RangeError) bi = BigInt(Math.trunc(x));\n  else throw e;\n}","preventionTips":["Check Number.isInteger(x) before every BigInt(x) conversion","Never feed division or Math results straight into BigInt","Scale decimals into integers (minor units) before BigInt","Prefer BigInt literals (5n) and string parsing for known-integer values"],"tags":["javascript","bigint","range-error","numeric-conversion"],"backgroundTag":"invalid-argument-value","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"}