简体   繁体   English

格式为 Javascript 中的货币

[英]Format as currency in Javascript

I have a variable which is made up of a multiplication of 2 more variables我有一个变量,它由另外两个变量的乘积组成

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

Is it possible, and if so how, or what is the function called to format this as currency?是否有可能,如果可以,或者如何调用 function 以将其格式化为货币? I've been googling only I cant find a function and the only ways I've seen are really, really long winded我一直在谷歌搜索,但我找不到 function 而我所看到的唯一方法是真的,真的很啰嗦

Taken from here: http://javascript.internet.com/forms/currency-format.html .取自这里: http://javascript.internet.com/forms/currency-format.html I've 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);
}

If you are looking for a quick function which would format your number (for example: 1234.5678) to something like: $1234.57 you may use.toFixed(..) method:如果您正在寻找一个快速的 function 它将您的号码(例如:1234.5678)格式化为:$1234.57,您可以使用.toFixed(..) 方法:

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

The toFixed function takes an integer value as the argument which means the number of trailing decimals. toFixed function 将 integer 值作为参数,表示尾随小数的数量。 More information about this method can be found at http://www.w3schools.com/jsref/jsref_tofixed.asp .有关此方法的更多信息,请访问http://www.w3schools.com/jsref/jsref_tofixed.asp

Otherwise, if you would like to have your output format as: $1,234.57 you need to implement your own function for this.否则,如果您希望 output 格式为:$1,234.57,您需要为此实现自己的 function。 Below are two links with implementation:以下是两个实现链接:

How abt using jQuery globalization plugin from microsoft?如何使用微软的 jQuery 全球化插件? https://github.com/nje/jquery-glob https://github.com/nje/jquery-glob

May not be perfect, but works for me.可能并不完美,但对我有用。

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

There are no in-built functions for formatting currency in Javascript. Javascript 中没有用于格式化货币的内置函数。

There are a number of scripts that you can find online that will help you with writing your own, however, here's one:您可以在网上找到许多脚本来帮助您编写自己的脚本,但是,这里有一个:

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

Google "javascript currency".谷歌“javascript 货币”。

Now there's Intl.NumberFormat that can easily format currency or other numbers to locales.现在有Intl.NumberFormat可以轻松地将货币或其他数字格式化为语言环境。

Using your EstimatedTotal example and USD, you can do:使用您的EstimatedTotal示例和美元,您可以执行以下操作:

const formattedTotal = 
new Intl.NumberFormat('en-US', { 
    style: 'currency', 
    currency: 'USD'
}).format(EstimatedTotal));

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM