Return to Snippet

Revision: 31419
at September 4, 2010 16:10 by henriquemoody


Updated Code
/**
 * Object to currency manipulation.
 *
 * @author  Henrique Moody <[email protected]>
 */
Currency  = {

    /**
     * The currency simbal
     *
     * @type {String}
     */
    simbal : '$',

    /**
     * Cents separator.
     *
     * @type {String}
     */
    centsSeparator : '.',

    /**
     * Thousands separator.
     *
     * @type {String}
     */
    thousandsSeparator : ',',

    /**
     * Turns a number into a currency string.
     *
     * Based on http://javascript.internet.com/forms/currency-format.html
     *
     * @author Cyanide_7 <[email protected]>
     * @param {Number} number
     * @return {String}
     */
    numberToCurrency : function (number)
    {
        if (this.isCurrency(number)) {
            return number;
        }
        if(isNaN(number))
            number = "0";
        var sign = (number == (number = Math.abs(number)));
        number = Math.floor(number*100+0.50000000001);
        var cents = number%100;
        number = Math.floor(number/100).toString();
        if(cents < 10)
            cents = "0" + cents;
        for (var i = 0; i < Math.floor((number.length-(1+i))/3); i++)
            number = number.substring(0,number.length-(4*i+3))+ this.thousandsSeparator+
            number.substring(number.length-(4*i+3));
        return (((sign)?'':'-') + this.simbal + number + this.centsSeparator + cents);
    },

    /**
     * Turns a currency string into a number.
     *
     * @param {String} currency
     * @return {Number}
     */
    currencyToNumber : function (currency)
    {
        if (!isNaN(currency)) {
            return currency;
        }
        currency    = currency.toString()
                              .replace(this.simbal, '')
                              .replace(this.thousandsSeparator, '', 'g')
                              .replace(this.centsSeparator, '.');
        return parseFloat(currency);
    },

    /**
     *  Checks if "currency" is a valid currency.
     *
     * @return {Boolean}
     */
    isCurrency : function (currency)
    {
        var simbal  = this.simbal.replace('\$', '\\$');
        var regex   = '^' + simbal +
                      '[0-9]{1,3}(([' + this.thousandsSeparator + '][0-9]{3})+)?' +
                      '[' +this.centsSeparator + '][0-9]{2}$';
        regex   = new RegExp(regex);
        return regex.test(currency);
    }

};

/**
 * Turns a number into a currency string.
 *
 * Allows transform any number into currency.
 * <pre>
 * var myNumber     = 2100.58;
 * var myCurrency   = myNumber.<b>toCurrency()</b>; <i>//$2,100.58</i>
 * </pre>
 * @return {String}
 */
Number.prototype.toCurrency = function ()
{
    return Currency.numberToCurrency(this);
};

/**
 * Checks if the string is a valid currency.
 *
 * Allows checks if any string is a valid currency.
 * 
 * <pre>
 * var myString     = "$1,159,654.00";
 * alert(myString.isCurrency());// true
 * </pre>
 * @return {String}
 */
String.prototype.isCurrency = function ()
{
    return Currency.isCurrency(this);
}

Revision: 31418
at September 4, 2010 09:44 by henriquemoody


Updated Code
/**
 * Object to currency manipulation.
 *
 * @author  Henrique Moody <[email protected]>
 */
Currency  = {

    /**
     * The currency simbal
     *
     * @type {String}
     */
    simbal : '$',

    /**
     * Cents separator.
     *
     * @type {String}
     */
    centsSeparator : '.',

    /**
     * Thousands separator.
     *
     * @type {String}
     */
    thousandsSeparator : ',',

    /**
     * Turns a number into a currency string.
     *
     * Based on http://javascript.internet.com/forms/currency-format.html
     * 
     * @author Cyanide_7 <[email protected]>
     * @param {Number} number
     * @return {String}
     */
    numberToCurrency : function (number)
    {
        if (this.isCurrency(number)) {
            return number;
        }
        if(isNaN(number))
            number = "0";
        var sign = (number == (number = Math.abs(number)));
        number = Math.floor(number*100+0.50000000001);
        var cents = number%100;
        number = Math.floor(number/100).toString();
        if(cents < 10)
            cents = "0" + cents;
        for (var i = 0; i < Math.floor((number.length-(1+i))/3); i++)
            number = number.substring(0,number.length-(4*i+3))+ this.thousandsSeparator+
            number.substring(number.length-(4*i+3));
        return (((sign)?'':'-') + this.simbal + number + this.centsSeparator + cents);
    },

    /**
     * Turns a currency string into a number.
     *
     * @param {String} currency
     * @return {Number}
     */
    currencyToNumber : function (currency)
    {
        if (!isNaN(currency)) {
            return currency;
        }
        currency    = currency.toString().replace(this.simbal, '');
        currency    = currency.replace(this.thousandsSeparator, '', 'g');
        currency    = currency.replace(this.centsSeparator, '.');
        return parseFloat(currency);
    },
    
    /**
     *  Checks if "currency" is a valid currency.
     * 
     * @return {Boolean}
     */
    isCurrency : function (currency)
    {
        var simbal  = this.simbal.replace('\$', '\\$');
        var regex   = '^' + simbal + 
                      '[0-9]{1,3}(([' + this.thousandsSeparator + '][0-9]{3})+)?' +
                      '[' +this.centsSeparator + '][0-9]{2}$';
        regex   = new RegExp(regex);
        return regex.test(currency);
    }

};

/**
 * Turns a number into a currency string.
 *
 * Allows transform any number in currency.
 * <pre>
 * var myNumber     = 2100.58;
 * var myCurrency   = myNumber.<b>toCurrency()</b>; <i>//$2,100.58</i>
 * </pre>
 * @return {String}
 */
Number.prototype.toCurrency = function ()
{
    return Currency.numberToCurrency(this);
};

Revision: 31417
at September 4, 2010 09:42 by henriquemoody


Initial Code
/**
 * Object to currency manipulation.
 *
 * @author  Henrique Moody <[email protected]>
 */
Currency  = {

    /**
     * The currency simbal
     *
     * @type {String}
     */
    simbal : '$',

    /**
     * Cents separator.
     *
     * @type {String}
     */
    centsSeparator : '.',

    /**
     * Thousands separator.
     *
     * @type {String}
     */
    thousandsSeparator : ',',

    /**
     * Turns a number into a currency string.
     *
     * Based on http://javascript.internet.com/forms/currency-format.html
     * 
     * @author Cyanide_7 <[email protected]>
     * @param {Number} number
     * @return {String}
     */
    numberToCurrency : function (number)
    {
        if (this.isCurrency(number)) {
            return number;
        }
        if(isNaN(number))
            number = "0";
        var sign = (number == (number = Math.abs(number)));
        number = Math.floor(number*100+0.50000000001);
        var cents = number%100;
        number = Math.floor(number/100).toString();
        if(cents < 10)
            cents = "0" + cents;
        for (var i = 0; i < Math.floor((number.length-(1+i))/3); i++)
            number = number.substring(0,number.length-(4*i+3))+ this.thousandsSeparator+
            number.substring(number.length-(4*i+3));
        var currency = (((sign)?'':'-') + this.simbal + number + this.centsSeparator + cents);
        this.isCurrency(currency);
        return currency;
    },

    /**
     * Turns a currency string into a number.
     *
     * @param {String} currency
     * @return {Number}
     */
    currencyToNumber : function (currency)
    {
        if (!isNaN(currency)) {
            return currency;
        }
        currency    = currency.toString().replace(this.simbal, '');
        currency    = currency.replace(this.thousandsSeparator, '', 'g');
        currency    = currency.replace(this.centsSeparator, '.');
        return parseFloat(currency);
    },
    
    /**
     *  Checks if "currency" is a valid currency.
     * 
     * @return {Boolean}
     */
    isCurrency : function (currency)
    {
        var simbal  = this.simbal.replace('\$', '\\$');
        var regex   = '^' + simbal + 
                      '[0-9]{1,3}(([' + this.thousandsSeparator + '][0-9]{3})+)?' +
                      '[' +this.centsSeparator + '][0-9]{2}$';
        regex   = new RegExp(regex);
        return regex.test(currency);
    }

};

/**
 * Turns a number into a currency string.
 *
 * Allows transform any number in currency.
 * <pre>
 * var myNumber     = 2100.58;
 * var myCurrency   = myNumber.<b>toCurrency()</b>; <i>//$2,100.58</i>
 * </pre>
 * @return {String}
 */
Number.prototype.toCurrency = function ()
{
    return Currency.numberToCurrency(this);
};

Initial URL


Initial Description


Initial Title
Convert number to currency and vice-versa

Initial Tags
javascript, number

Initial Language
JavaScript