Format as Javascript Currency

I have a variable consisting of multiplication by two variables

 var EstimatedTotal =  GetNumeric(ServiceLevel) * GetNumeric(EstimatedCoreHours);

Is this possible, and if so, how or what function is designed to format this currency? I searched only Google, but I could not find the function, and the only ways I saw were really very long.

+3
source share
5 answers

Taken from here: http://javascript.internet.com/forms/currency-format.html . I used it and it works well.

function formatCurrency(num)
{
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
    {
        num = "0";
    }

    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();

    if (cents < 10)
    {
        cents = "0" + cents;
    }
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
    {
        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    }

    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}
+7
source

If you are looking for a quick function that would format your number (for example: 1234.5678) by about $ 1234.57, you can use the .toFixed (..) method:

EstimatedTotal = "$" + EstimatedTotal.toFixed(2);

toFixed , . http://www.w3schools.com/jsref/jsref_tofixed.asp.

, , : $1,234.57, . :

+7
+3

, .

if (String.prototype.ToCurrencyFormat == null) 
    String.prototype.ToCurrencyFormat = function() 
    { 
        if (isNaN(this * 1) == false)
            return "$" + (this * 1).toFixed(2); 
    }
+1

Javascript.

There are several scripts that you can find on the Internet that will help you write your own, however, one of them:

http://javascript.internet.com/forms/currency-format.html

Google "javascript currency".

0
source

All Articles