andrew4582

NumberFormat js

Dec 2nd, 2011
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. function NumberFormat(num, inputDecimal) {
  4.     this.VERSION = 'Number Format v1.5.4';
  5.     this.COMMA = ',';
  6.     this.PERIOD = '.';
  7.     this.DASH = '-';
  8.     this.LEFT_PAREN = '(';
  9.     this.RIGHT_PAREN = ')';
  10.     this.LEFT_OUTSIDE = 0;
  11.     this.LEFT_INSIDE = 1;
  12.     this.RIGHT_INSIDE = 2;
  13.     this.RIGHT_OUTSIDE = 3;
  14.     this.LEFT_DASH = 0;
  15.     this.RIGHT_DASH = 1;
  16.     this.PARENTHESIS = 2;
  17.     this.NO_ROUNDING = -1
  18.     this.num;
  19.     this.numOriginal;
  20.     this.hasSeparators = false;
  21.     this.separatorValue;
  22.     this.inputDecimalValue;
  23.     this.decimalValue;
  24.     this.negativeFormat;
  25.     this.negativeRed;
  26.     this.hasCurrency;
  27.     this.currencyPosition;
  28.     this.currencyValue;
  29.     this.places;
  30.     this.roundToPlaces;
  31.     this.truncate;
  32.     this.setNumber = setNumberNF;
  33.     this.toUnformatted = toUnformattedNF;
  34.     this.setInputDecimal = setInputDecimalNF;
  35.     this.setSeparators = setSeparatorsNF;
  36.     this.setCommas = setCommasNF;
  37.     this.setNegativeFormat = setNegativeFormatNF;
  38.     this.setNegativeRed = setNegativeRedNF;
  39.     this.setCurrency = setCurrencyNF;
  40.     this.setCurrencyPrefix = setCurrencyPrefixNF;
  41.     this.setCurrencyValue = setCurrencyValueNF;
  42.     this.setCurrencyPosition = setCurrencyPositionNF;
  43.     this.setPlaces = setPlacesNF;
  44.     this.toFormatted = toFormattedNF;
  45.     this.toPercentage = toPercentageNF;
  46.     this.getOriginal = getOriginalNF;
  47.     this.moveDecimalRight = moveDecimalRightNF;
  48.     this.moveDecimalLeft = moveDecimalLeftNF;
  49.     this.getRounded = getRoundedNF;
  50.     this.preserveZeros = preserveZerosNF;
  51.     this.justNumber = justNumberNF;
  52.     this.expandExponential = expandExponentialNF;
  53.     this.getZeros = getZerosNF;
  54.     this.moveDecimalAsString = moveDecimalAsStringNF;
  55.     this.moveDecimal = moveDecimalNF;
  56.     this.addSeparators = addSeparatorsNF;
  57.     if (inputDecimal == null) {
  58.         this.setNumber(num, this.PERIOD);
  59.     } else {
  60.         this.setNumber(num, inputDecimal);
  61.     }
  62.     this.setCommas(true);
  63.     this.setNegativeFormat(this.LEFT_DASH);
  64.     this.setNegativeRed(false);
  65.     this.setCurrency(false);
  66.     this.setCurrencyPrefix('$');
  67.     this.setPlaces(2);
  68. }
  69.  
  70. function setInputDecimalNF(val) {
  71.     this.inputDecimalValue = val;
  72. }
  73.  
  74. function setNumberNF(num, inputDecimal) {
  75.     if (inputDecimal != null) {
  76.         this.setInputDecimal(inputDecimal);
  77.     }
  78.     this.numOriginal = num;
  79.     this.num = this.justNumber(num);
  80. }
  81.  
  82. function toUnformattedNF() {
  83.     return (this.num);
  84. }
  85.  
  86. function getOriginalNF() {
  87.     return (this.numOriginal);
  88. }
  89.  
  90. function setNegativeFormatNF(format) {
  91.     this.negativeFormat = format;
  92. }
  93.  
  94. function setNegativeRedNF(isRed) {
  95.     this.negativeRed = isRed;
  96. }
  97.  
  98. function setSeparatorsNF(isC, separator, decimal) {
  99.     this.hasSeparators = isC;
  100.     if (separator == null) separator = this.COMMA;
  101.     if (decimal == null) decimal = this.PERIOD;
  102.     if (separator == decimal) {
  103.         this.decimalValue = (decimal == this.PERIOD) ? this.COMMA : this.PERIOD;
  104.     } else {
  105.         this.decimalValue = decimal;
  106.     }
  107.     this.separatorValue = separator;
  108. }
  109.  
  110. function setCommasNF(isC) {
  111.     this.setSeparators(isC, this.COMMA, this.PERIOD);
  112. }
  113.  
  114. function setCurrencyNF(isC) {
  115.     this.hasCurrency = isC;
  116. }
  117.  
  118. function setCurrencyValueNF(val) {
  119.     this.currencyValue = val;
  120. }
  121.  
  122. function setCurrencyPrefixNF(cp) {
  123.     this.setCurrencyValue(cp);
  124.     this.setCurrencyPosition(this.LEFT_OUTSIDE);
  125. }
  126.  
  127. function setCurrencyPositionNF(cp) {
  128.     this.currencyPosition = cp
  129. }
  130.  
  131. function setPlacesNF(p, tr) {
  132.     this.roundToPlaces = !(p == this.NO_ROUNDING);
  133.     this.truncate = (tr != null && tr);
  134.     this.places = (p < 0) ? 0 : p;
  135. }
  136.  
  137. function addSeparatorsNF(nStr, inD, outD, sep) {
  138.     nStr += '';
  139.     var dpos = nStr.indexOf(inD);
  140.     var nStrEnd = '';
  141.     if (dpos != -1) {
  142.         nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
  143.         nStr = nStr.substring(0, dpos);
  144.     }
  145.     var rgx = /(\d+)(\d{3})/;
  146.     while (rgx.test(nStr)) {
  147.         nStr = nStr.replace(rgx, '$1' + sep + '$2');
  148.     }
  149.     return nStr + nStrEnd;
  150. }
  151.  
  152. function toFormattedNF() {
  153.     var pos;
  154.     var nNum = this.num;
  155.     var nStr;
  156.     var splitString = new Array(2);
  157.     if (this.roundToPlaces) {
  158.         nNum = this.getRounded(nNum);
  159.         nStr = this.preserveZeros(Math.abs(nNum));
  160.     } else {
  161.         nStr = this.expandExponential(Math.abs(nNum));
  162.     }
  163.     if (this.hasSeparators) {
  164.         nStr = this.addSeparators(nStr, this.PERIOD, this.decimalValue, this.separatorValue);
  165.     } else {
  166.         nStr = nStr.replace(new RegExp('\\' + this.PERIOD), this.decimalValue);
  167.     }
  168.     var c0 = '';
  169.     var n0 = '';
  170.     var c1 = '';
  171.     var n1 = '';
  172.     var n2 = '';
  173.     var c2 = '';
  174.     var n3 = '';
  175.     var c3 = '';
  176.     var negSignL = (this.negativeFormat == this.PARENTHESIS) ? this.LEFT_PAREN : this.DASH;
  177.     var negSignR = (this.negativeFormat == this.PARENTHESIS) ? this.RIGHT_PAREN : this.DASH;
  178.     if (this.currencyPosition == this.LEFT_OUTSIDE) {
  179.         if (nNum < 0) {
  180.             if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n1 = negSignL;
  181.             if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n2 = negSignR;
  182.         }
  183.         if (this.hasCurrency) c0 = this.currencyValue;
  184.     } else if (this.currencyPosition == this.LEFT_INSIDE) {
  185.         if (nNum < 0) {
  186.             if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n0 = negSignL;
  187.             if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n3 = negSignR;
  188.         }
  189.         if (this.hasCurrency) c1 = this.currencyValue;
  190.     } else if (this.currencyPosition == this.RIGHT_INSIDE) {
  191.         if (nNum < 0) {
  192.             if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n0 = negSignL;
  193.             if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n3 = negSignR;
  194.         }
  195.         if (this.hasCurrency) c2 = this.currencyValue;
  196.     } else if (this.currencyPosition == this.RIGHT_OUTSIDE) {
  197.         if (nNum < 0) {
  198.             if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n1 = negSignL;
  199.             if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n2 = negSignR;
  200.         }
  201.         if (this.hasCurrency) c3 = this.currencyValue;
  202.     }
  203.     nStr = c0 + n0 + c1 + n1 + nStr + n2 + c2 + n3 + c3;
  204.     if (this.negativeRed && nNum < 0) {
  205.         nStr = '<font color="red">' + nStr + '</font>';
  206.     }
  207.     return (nStr);
  208. }
  209.  
  210. function toPercentageNF() {
  211.     nNum = this.num * 100;
  212.     nNum = this.getRounded(nNum);
  213.     return nNum + '%';
  214. }
  215.  
  216. function getZerosNF(places) {
  217.     var extraZ = '';
  218.     var i;
  219.     for (i = 0; i < places; i++) {
  220.         extraZ += '0';
  221.     }
  222.     return extraZ;
  223. }
  224.  
  225. function expandExponentialNF(origVal) {
  226.     if (isNaN(origVal)) return origVal;
  227.     var newVal = parseFloat(origVal) + '';
  228.     var eLoc = newVal.toLowerCase().indexOf('e');
  229.     if (eLoc != -1) {
  230.         var plusLoc = newVal.toLowerCase().indexOf('+');
  231.         var negLoc = newVal.toLowerCase().indexOf('-', eLoc);
  232.         var justNumber = newVal.substring(0, eLoc);
  233.         if (negLoc != -1) {
  234.             var places = newVal.substring(negLoc + 1, newVal.length);
  235.             justNumber = this.moveDecimalAsString(justNumber, true, parseInt(places));
  236.         } else {
  237.             if (plusLoc == -1) plusLoc = eLoc;
  238.             var places = newVal.substring(plusLoc + 1, newVal.length);
  239.             justNumber = this.moveDecimalAsString(justNumber, false, parseInt(places));
  240.         }
  241.         newVal = justNumber;
  242.     }
  243.     return newVal;
  244. }
  245.  
  246. function moveDecimalRightNF(val, places) {
  247.     var newVal = '';
  248.     if (places == null) {
  249.         newVal = this.moveDecimal(val, false);
  250.     } else {
  251.         newVal = this.moveDecimal(val, false, places);
  252.     }
  253.     return newVal;
  254. }
  255.  
  256. function moveDecimalLeftNF(val, places) {
  257.     var newVal = '';
  258.     if (places == null) {
  259.         newVal = this.moveDecimal(val, true);
  260.     } else {
  261.         newVal = this.moveDecimal(val, true, places);
  262.     }
  263.     return newVal;
  264. }
  265.  
  266. function moveDecimalAsStringNF(val, left, places) {
  267.     var spaces = (arguments.length < 3) ? this.places : places;
  268.     if (spaces <= 0) return val;
  269.     var newVal = val + '';
  270.     var extraZ = this.getZeros(spaces);
  271.     var re1 = new RegExp('([0-9.]+)');
  272.     if (left) {
  273.         newVal = newVal.replace(re1, extraZ + '$1');
  274.         var re2 = new RegExp('(-?)([0-9]*)([0-9]{' + spaces + '})(\\.?)');
  275.         newVal = newVal.replace(re2, '$1$2.$3');
  276.     } else {
  277.         var reArray = re1.exec(newVal);
  278.         if (reArray != null) {
  279.             newVal = newVal.substring(0, reArray.index) + reArray[1] + extraZ + newVal.substring(reArray.index + reArray[0].length);
  280.         }
  281.         var re2 = new RegExp('(-?)([0-9]*)(\\.?)([0-9]{' + spaces + '})');
  282.         newVal = newVal.replace(re2, '$1$2$4.');
  283.     }
  284.     newVal = newVal.replace(/\.$/, '');
  285.     return newVal;
  286. }
  287.  
  288. function moveDecimalNF(val, left, places) {
  289.     var newVal = '';
  290.     if (places == null) {
  291.         newVal = this.moveDecimalAsString(val, left);
  292.     } else {
  293.         newVal = this.moveDecimalAsString(val, left, places);
  294.     }
  295.     return parseFloat(newVal);
  296. }
  297.  
  298. function getRoundedNF(val) {
  299.     val = this.moveDecimalRight(val);
  300.     if (this.truncate) {
  301.         val = val >= 0 ? Math.floor(val) : Math.ceil(val);
  302.     } else {
  303.         val = Math.round(val);
  304.     }
  305.     val = this.moveDecimalLeft(val);
  306.     return val;
  307. }
  308.  
  309. function preserveZerosNF(val) {
  310.     var i;
  311.     val = this.expandExponential(val);
  312.     if (this.places <= 0) return val;
  313.     var decimalPos = val.indexOf('.');
  314.     if (decimalPos == -1) {
  315.         val += '.';
  316.         for (i = 0; i < this.places; i++) {
  317.             val += '0';
  318.         }
  319.     } else {
  320.         var actualDecimals = (val.length - 1) - decimalPos;
  321.         var difference = this.places - actualDecimals;
  322.         for (i = 0; i < difference; i++) {
  323.             val += '0';
  324.         }
  325.     }
  326.     return val;
  327. }
  328.  
  329. function justNumberNF(val) {
  330.     newVal = val + '';
  331.     var isPercentage = false;
  332.     if (newVal.indexOf('%') != -1) {
  333.         newVal = newVal.replace(/\%/g, '');
  334.         isPercentage = true;
  335.     }
  336.     var re = new RegExp('[^\\' + this.inputDecimalValue + '\\d\\-\\+\\(\\)eE]', 'g');
  337.     newVal = newVal.replace(re, '');
  338.     var tempRe = new RegExp('[' + this.inputDecimalValue + ']', 'g');
  339.     var treArray = tempRe.exec(newVal);
  340.     if (treArray != null) {
  341.         var tempRight = newVal.substring(treArray.index + treArray[0].length);
  342.         newVal = newVal.substring(0, treArray.index) + this.PERIOD + tempRight.replace(tempRe, '');
  343.     }
  344.     if (newVal.charAt(newVal.length - 1) == this.DASH) {
  345.         newVal = newVal.substring(0, newVal.length - 1);
  346.         newVal = '-' + newVal;
  347.     } else if (newVal.charAt(0) == this.LEFT_PAREN && newVal.charAt(newVal.length - 1) == this.RIGHT_PAREN) {
  348.         newVal = newVal.substring(1, newVal.length - 1);
  349.         newVal = '-' + newVal;
  350.     }
  351.     newVal = parseFloat(newVal);
  352.     if (!isFinite(newVal)) {
  353.         newVal = 0;
  354.     }
  355.     if (isPercentage) {
  356.         newVal = this.moveDecimalLeft(newVal, 2);
  357.     }
  358.     return newVal;
  359. }
Advertisement
Add Comment
Please, Sign In to add comment