Dashiva

IGM Number format

Jan 24th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. Number formatting for IdleGameMaker.
  3. */
  4. (function(){
  5.   var MODULE_KEY = 'numberformat_gw4y480hjear';
  6.   if (window[MODULE_KEY]) {
  7.     return;
  8.   }
  9.   var oldFunction = window.Beautify;
  10.   if (!oldFunction) {
  11.     alert('Could not find Beautify() function!');
  12.     return;
  13.   }
  14.   window.Beautify = function(what, floats) {
  15.     if (!isFinite(what) || isNaN(what)) {
  16.       return '' + what;
  17.     }
  18.     if (what == 0) {
  19.       return '0';
  20.     }
  21.     var prefix = what < 0 ? '-' : '';
  22.     var base = Math.abs(what);
  23.     if (base < 1) {
  24.       return base.toLocaleString();
  25.     }
  26.     if (base < 1e12) {
  27.       var a = Math.floor(base).toLocaleString();
  28.       var b = base % 1;
  29.       if (b && floats && base < 1e6) {
  30.         a += b.toFixed(floats).slice(1);
  31.       }
  32.       return prefix + a;
  33.     }
  34.     var exp = 0;
  35.     while (base >= 1e3) {
  36.       base = base / 1e3;
  37.       exp += 3;
  38.     }
  39.     return prefix + base.toFixed(3) + '_e' + exp;
  40.   };
  41.   window[MODULE_KEY] = oldFunction;
  42. })();
Advertisement
Add Comment
Please, Sign In to add comment