Created
April 10, 2021 23:45
-
-
Save Hafthor/0a60f918d50113600d7c67252e68a02d to your computer and use it in GitHub Desktop.
non-decimal non-integer stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// like parseFloat, but takes an optional radix | |
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); | |
} | |
// like Number..toFixed, but takes an optional radix | |
function toFixed(n, p, r) { | |
r = (r||10)|0; | |
const m = parseInt('1' + Array(Math.abs((p||0)|0) + 1).join('0'), r), d = 1/m; | |
if (p>0) ({m, d}={m:d, d:m}); | |
return (Math.round(parseFloatWithRadix(n, r) * d) * m).toString(r); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment