Last active
May 16, 2018 16:05
-
-
Save Canop/90b2b85bbc454650bc6636f869783c3e to your computer and use it in GitHub Desktop.
float numbers formatting
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
const float = function(v, p=2){ | |
let absv = Math.abs(v); | |
if (absv>10**(p+3) || absv<10**-p) return v.toExponential(p).replace(/0+e/, 'e'); | |
return v.toFixed(p).replace(/\.0+$|0+$/, ''); | |
} | |
// run the following to test the float function and | |
// compare the values with common formatting functions | |
const bodys = [ | |
"v.toString()", | |
"v.toFixed(3)", | |
"v.toExponential(3)", | |
"v.toPrecision(3)", | |
"float(v, 2)", | |
"float(v, 3)", | |
]; | |
const funs = bodys.map(b=>(v=>eval(b))); | |
const tests = [ | |
1, 12, 123, 1234, 12345, 123456, 1234567, 123456789, 1234567890, | |
.1, .12, .123, .1234, | |
100, 100.1, 100.01, 100.001, 100.0001, 100.00001, | |
1.99, 1.999, 1.9999, 1.99999999, | |
.047, .0047, .00047, .000047, .000000047, .0000000000000047, | |
NaN | |
]; | |
[bodys, bodys.map(_=>"-:")].concat(tests.map(t=> | |
funs.map(f=>f(t)) | |
)).forEach(row => console.log(row.map(c=>c.padStart(20, " ")).join(" | "))); | |
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
v.toString() | v.toFixed(3) | v.toExponential(3) | v.toPrecision(3) | float(v, 2) | float(v, 3) | |
1 | 1.000 | 1.000e+0 | 1.00 | 1 | 1 | |
12 | 12.000 | 1.200e+1 | 12.0 | 12 | 12 | |
123 | 123.000 | 1.230e+2 | 123 | 123 | 123 | |
1234 | 1234.000 | 1.234e+3 | 1.23e+3 | 1234 | 1234 | |
12345 | 12345.000 | 1.235e+4 | 1.23e+4 | 12345 | 12345 | |
123456 | 123456.000 | 1.235e+5 | 1.23e+5 | 1.23e+5 | 123456 | |
1234567 | 1234567.000 | 1.235e+6 | 1.23e+6 | 1.23e+6 | 1.235e+6 | |
123456789 | 123456789.000 | 1.235e+8 | 1.23e+8 | 1.23e+8 | 1.235e+8 | |
1234567890 | 1234567890.000 | 1.235e+9 | 1.23e+9 | 1.23e+9 | 1.235e+9 | |
0.1 | 0.100 | 1.000e-1 | 0.100 | 0.1 | 0.1 | |
0.12 | 0.120 | 1.200e-1 | 0.120 | 0.12 | 0.12 | |
0.123 | 0.123 | 1.230e-1 | 0.123 | 0.12 | 0.123 | |
0.1234 | 0.123 | 1.234e-1 | 0.123 | 0.12 | 0.123 | |
100 | 100.000 | 1.000e+2 | 100 | 100 | 100 | |
100.1 | 100.100 | 1.001e+2 | 100 | 100.1 | 100.1 | |
100.01 | 100.010 | 1.000e+2 | 100 | 100.01 | 100.01 | |
100.001 | 100.001 | 1.000e+2 | 100 | 100 | 100.001 | |
100.0001 | 100.000 | 1.000e+2 | 100 | 100 | 100 | |
100.00001 | 100.000 | 1.000e+2 | 100 | 100 | 100 | |
1.99 | 1.990 | 1.990e+0 | 1.99 | 1.99 | 1.99 | |
1.999 | 1.999 | 1.999e+0 | 2.00 | 2 | 1.999 | |
1.9999 | 2.000 | 2.000e+0 | 2.00 | 2 | 2 | |
1.99999999 | 2.000 | 2.000e+0 | 2.00 | 2 | 2 | |
0.047 | 0.047 | 4.700e-2 | 0.0470 | 0.05 | 0.047 | |
0.0047 | 0.005 | 4.700e-3 | 0.00470 | 4.7e-3 | 0.005 | |
0.00047 | 0.000 | 4.700e-4 | 0.000470 | 4.7e-4 | 4.7e-4 | |
0.000047 | 0.000 | 4.700e-5 | 0.0000470 | 4.7e-5 | 4.7e-5 | |
4.7e-8 | 0.000 | 4.700e-8 | 4.70e-8 | 4.7e-8 | 4.7e-8 | |
4.7e-15 | 0.000 | 4.700e-15 | 4.70e-15 | 4.7e-15 | 4.7e-15 | |
NaN | NaN | NaN | NaN | NaN | NaN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment