-
-
Save phattranky/e6624f852d90361bf8acc4661d77b374 to your computer and use it in GitHub Desktop.
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
function formatN (n) { | |
var nn = n.toExponential(2).split(/e/); | |
var u = Math.floor(+nn[1] / 3); | |
return nn[0] * Math.pow(10, +nn[1] - u * 3) + ['p', 'n', 'u', 'm', '', 'k', 'M', 'G', 'T'][u+4]; | |
} | |
var array = [1e7, 1e6, 2345, 100, 10, 1, 1e-2, 1e-3, 1e-5, 1e-6, 256e-9]; | |
for (var i = 0, len = array.length; i < len; i++) { | |
var n = array[i]; | |
var formatted = formatN(n); | |
console.log(n, formatted); | |
} |
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
10000000 '10M' | |
1000000 '1M' | |
2345 '2.35k' | |
100 '100' | |
10 '10' | |
1 '1' | |
0.01 '10m' | |
0.001 '1m' | |
0.00001 '10u' | |
0.000001 '1u' | |
2.56e-7 '256n' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment