在 Javascript 中格式化为货币

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6124644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:34:44  来源:igfitidea点击:

Format as currency in Javascript

javascript

提问by Liam

I have a variable which is made up of a multiplication of 2 more variables

我有一个变量,它由 2 个以上变量的乘积组成

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

Is it possible, and if so how, or what is the function called to format this as currency? I've been googling only I cant find a function and the only ways I've seen are really, really long winded

是否可能,如果可能,如何或调用什么函数将其格式化为货币?我一直在谷歌搜索,只是我找不到一个功能,我看到的唯一方法真的非常冗长

回答by James Hill

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

取自此处:http: //javascript.internet.com/forms/currency-format.html。我已经使用过它并且效果很好。

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);
}

回答by dmitry7

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:

如果您正在寻找一个快速函数,它将您的号码(例如: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. More information about this method can be found at http://www.w3schools.com/jsref/jsref_tofixed.asp.

toFixed 函数将一个整数值作为参数,这意味着尾随小数的数量。有关此方法的更多信息,请访问 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. Below are two links with implementation:

否则,如果您希望输出格式为:$1,234.57,您需要为此实现您自己的函数。以下是两个与实现的链接:

回答by a6hi5h3k

How abt using jQuery globalization plugin from microsoft? https://github.com/nje/jquery-glob

如何使用微软的 jQuery 全球化插件? https://github.com/nje/jquery-glob

回答by Brett Weber

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); 
    }

回答by James

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 货币”。