Advertisement
Tellurium

Zyzzyzus' number shortener with full names

Jul 15th, 2014
3,305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. nums = ['thousand','million','billion','trillion','quadrillion','quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion', 'quindecillion', 'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion'];
  2.  
  3.  
  4. function Beautify(num,floats)
  5. {
  6. if (!isFinite(num)) return 'Infinity'
  7. if(num < 1e3 || num >= parseFloat('1e' + 3*(nums.length))) return Math.round(num)
  8. var i = 0;
  9. while(num >= 1000)
  10. {
  11. num/=1000;
  12. i++;
  13. }
  14. num = Math.round(num*1000)/1000;
  15. if(num>=1000)
  16. {
  17. num/=1000;
  18. i++;
  19. num = Math.round(num*1000)/1000;
  20. } //deals with rounding errors
  21. num = num.toString();
  22. if(num.indexOf('.') == -1) num += '.000'
  23. else
  24. {
  25. dec = num.indexOf('.');
  26. while(num.slice(dec,num.length).length < 4) num += '0' //adds trailing 0s (if needed) to stop the numbers jumping around.
  27. }
  28. return num +" "+ nums[i - 1]
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement