【问题标题】:How to format a number into standard currency format (comma seperated digits)如何将数字格式化为标准货币格式(逗号分隔的数字)
【发布时间】:2012-04-21 03:25:11
【问题描述】:

我想知道如何使用 Java 代码或 jQuery 将像 2000000 这样的数字格式化为标准货币格式,即 2,000,000? Javacode 是最好的,但 jQuery 也可以 谢谢

【问题讨论】:

标签: java jquery format currency number-formatting


【解决方案1】:

看看jQuery number formatter。大多数格式已经存在,您可以定义自己的格式。

【讨论】:

  • 感谢您帮助我喜欢 jQuery 解决方案,因为我只需要以这种方式显示它。不需要服务器端格式化的货币值。我在你引用的页面上添加了 jquery.numberformatter-1.2.2.js 并在示例中使用了函数,它显示错误,当我运行代码时未定义 HashTable。我添加了 jhashset.js 但仍然出现错误。
【解决方案2】:

使用 NumberFormat.getCurrencyInstance()。考虑一下:

int n = 2000000;

NumberFormat usa = NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat uk = NumberFormat.getCurrencyInstance(Locale.UK);
NumberFormat germany = NumberFormat.getCurrencyInstance(Locale.GERMANY);

System.out.println(usa.format(n));
System.out.println(uk.format(n));
System.out.println(germany.format(n));

【讨论】:

  • 感谢 Opaco 的帮助,但我认为 jQuery 解决方案在我的情况下会更好。 Konerak 提供的解决方案让我遇到了“未定义哈希表”的问题,你也可以帮忙吗??
【解决方案3】:
/**
 * Format currency to $0.00 format
 * @param  {[string]} price
 * @return {[string]} Formated Price
 */
util.formatCurrency = function(price) {
    return '$' + String(price).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
};

【讨论】:

    猜你喜欢
    • 2012-08-18
    • 1970-01-01
    • 2020-07-18
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多