Skip to content

Instantly share code, notes, and snippets.

@Hafthor
Created April 10, 2021 23:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hafthor/0a60f918d50113600d7c67252e68a02d to your computer and use it in GitHub Desktop.
Save Hafthor/0a60f918d50113600d7c67252e68a02d to your computer and use it in GitHub Desktop.
non-decimal non-integer stuff
// 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