Second argument to parseFloat in JavaScript?
Here's a quickie version of parseFloat that does take a radix. It does NOT support scientific notation. Undefined behavior when given strings with digits outside the radix. Also behaves badly when given too many digits after the decimal point.
function parseFloatWithRadix(s, r) {
r = (r||10)|0;
const [b,a] = ((s||'0') + '.').split('.');
const l1 = parseInt('1'+(a||''), r).toString(r).length;
return parseInt(b, r) +
parseInt(a||'0', r) / parseInt('1' + Array(l1).join('0'), r);
}
parseFloatWithRadix('10.8', 16) gives 16.5
gist: https://gist.github.com/Hafthor/0a60f918d50113600d7c67252e68a02d
No, they're getting confused with parseInt()
, which can take a radix parameter. parseFloat()
, on the other hand, only accepts decimals. It might just be for consistency, as you should always pass a radix parameter to parseInt()
because it can treat numbers like 010
as octal, giving 8
rather than the correct 10
.
Here's the reference for parseFloat()
, versus parseInt()
.