Created
July 22, 2019 22:36
-
-
Save sturmenta/82e7560da7af3f94d2a184b56550e5b6 to your computer and use it in GitHub Desktop.
Parse big numbers
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 values = [ | |
{ K: 1000 }, | |
{ M: 1000000 }, | |
{ B: 1000000000 }, | |
{ Qd: 1000000000000 }, | |
{ Qt: 1000000000000000 }, | |
{ St: 1000000000000000000 }, | |
{ Sp: 1000000000000000000000 }, | |
{ O: 1000000000000000000000000 }, | |
{ N: 1000000000000000000000000000 }, | |
{ D: 1000000000000000000000000000000 }, | |
]; | |
export const formatBigNumber = vigitScore => { | |
let vigitScoreParsed; | |
values.forEach(value => { | |
const letter = Object.keys(value)[0]; | |
if (vigitScore / value[letter] < 999 && vigitScore / value[letter] > 1) | |
vigitScoreParsed = `${Math.round((vigitScore / value[letter]) * 100) / 100} ${letter}`; | |
}); | |
return vigitScoreParsed || Math.round(vigitScore * 100) / 100; | |
}; | |
// input - 3181.12139983 | |
// output - 3.18 K | |
// input - 350.12352345 | |
// output - 350.12 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment