在Javascript中以货币格式表示

问题描述

| 我有一个变量,该变量由2个其他变量的乘积组成
 var EstimatedTotal =  GetNumeric(ServiceLevel) * GetNumeric(EstimatedCoreHours);
可能吗?如果可以,怎么做?或者将其格式化为货币的函数叫什么?我一直在使用谷歌搜索,只是找不到函数,而且我见过的唯一方法确实非常漫长     

解决方法

从这里获取: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);
}
    ,如果您正在寻找一种快速的功能,可以将您的电话号码格式(例如:1234.5678)格式化为:$ 1234.57,则可以使用.toFixed(..)方法:
EstimatedTotal = \"$\" + EstimatedTotal.toFixed(2);
toFixed函数采用整数值作为参数,这意味着尾随小数位数。有关此方法的更多信息,请访问http://www.w3schools.com/jsref/jsref_tofixed.asp。 否则,如果您希望输出格式为:$ 1,234.57,则需要为此实现自己的功能。以下是实现的两个链接: http://javascript.internet.com/forms/currency-format.html http://www.web-source.net/web_development/currency_formatting.htm     ,如何从微软使用jQuery全球化插件? https://github.com/nje/jquery-glob     ,可能不是完美的,但对我有用。
if (String.prototype.ToCurrencyFormat == null) 
    String.prototype.ToCurrencyFormat = function() 
    { 
        if (isNaN(this * 1) == false)
            return \"$\" + (this * 1).toFixed(2); 
    }
    ,没有内置功能可用于格式化Javascript中的货币。 您可以在线找到许多脚本,这些脚本可以帮助您编写自己的脚本,但是这里有一个: http://javascript.internet.com/forms/currency-format.html Google \“ javascript货币\\”。     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...