Convert number to currency and vice-versa


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2.  * Object to currency manipulation.
  3.  *
  4.  * @author Henrique Moody <[email protected]>
  5.  */
  6. Currency = {
  7.  
  8. /**
  9.   * The currency simbal
  10.   *
  11.   * @type {String}
  12.   */
  13. simbal : '$',
  14.  
  15. /**
  16.   * Cents separator.
  17.   *
  18.   * @type {String}
  19.   */
  20. centsSeparator : '.',
  21.  
  22. /**
  23.   * Thousands separator.
  24.   *
  25.   * @type {String}
  26.   */
  27. thousandsSeparator : ',',
  28.  
  29. /**
  30.   * Turns a number into a currency string.
  31.   *
  32.   * Based on http://javascript.internet.com/forms/currency-format.html
  33.   *
  34.   * @author Cyanide_7 <[email protected]>
  35.   * @param {Number} number
  36.   * @return {String}
  37.   */
  38. numberToCurrency : function (number)
  39. {
  40. if (this.isCurrency(number)) {
  41. return number;
  42. }
  43. if(isNaN(number))
  44. number = "0";
  45. var sign = (number == (number = Math.abs(number)));
  46. number = Math.floor(number*100+0.50000000001);
  47. var cents = number%100;
  48. number = Math.floor(number/100).toString();
  49. if(cents < 10)
  50. cents = "0" + cents;
  51. for (var i = 0; i < Math.floor((number.length-(1+i))/3); i++)
  52. number = number.substring(0,number.length-(4*i+3))+ this.thousandsSeparator+
  53. number.substring(number.length-(4*i+3));
  54. return (((sign)?'':'-') + this.simbal + number + this.centsSeparator + cents);
  55. },
  56.  
  57. /**
  58.   * Turns a currency string into a number.
  59.   *
  60.   * @param {String} currency
  61.   * @return {Number}
  62.   */
  63. currencyToNumber : function (currency)
  64. {
  65. if (!isNaN(currency)) {
  66. return currency;
  67. }
  68. currency = currency.toString()
  69. .replace(this.simbal, '')
  70. .replace(this.thousandsSeparator, '', 'g')
  71. .replace(this.centsSeparator, '.');
  72. return parseFloat(currency);
  73. },
  74.  
  75. /**
  76.   * Checks if "currency" is a valid currency.
  77.   *
  78.   * @return {Boolean}
  79.   */
  80. isCurrency : function (currency)
  81. {
  82. var simbal = this.simbal.replace('\$', '\\$');
  83. var regex = '^' + simbal +
  84. '[0-9]{1,3}(([' + this.thousandsSeparator + '][0-9]{3})+)?' +
  85. '[' +this.centsSeparator + '][0-9]{2}$';
  86. regex = new RegExp(regex);
  87. return regex.test(currency);
  88. }
  89.  
  90. };
  91.  
  92. /**
  93.  * Turns a number into a currency string.
  94.  *
  95.  * Allows transform any number into currency.
  96.  * <pre>
  97.  * var myNumber = 2100.58;
  98.  * var myCurrency = myNumber.<b>toCurrency()</b>; <i>//$2,100.58</i>
  99.  * </pre>
  100.  * @return {String}
  101.  */
  102. Number.prototype.toCurrency = function ()
  103. {
  104. return Currency.numberToCurrency(this);
  105. };
  106.  
  107. /**
  108.  * Checks if the string is a valid currency.
  109.  *
  110.  * Allows checks if any string is a valid currency.
  111.  *
  112.  * <pre>
  113.  * var myString = "$1,159,654.00";
  114.  * alert(myString.isCurrency());// true
  115.  * </pre>
  116.  * @return {String}
  117.  */
  118. String.prototype.isCurrency = function ()
  119. {
  120. return Currency.isCurrency(this);
  121. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.