2012-04-08 23 views
1

我想知道如何将2000000这样的数字格式化为标准货币格式,例如使用Java代码或jQuery的2,000,000格式? Javacode是最好的,但jQuery的也将工作 感谢如何将数字格式化为标准货币格式(逗号分隔数字)

+0

的可能重复(http://stackoverflow.com/questions/8035245/how-can-i-format-a - 数字到固定的语言环境) – assylias 2012-04-08 09:00:25

+0

我需要任何内置的java或jQuery函数,所以我的工作很容易 – Rizstien 2012-04-08 09:00:26

回答

1

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

+1

感谢您帮助我喜欢jQuery解决方案,因为我只需要以这种方式显示它。服务器端格式化的货币值不是必需的。 我已经添加了jquery.numberformatter-1.2.2.js并且在被引用的页面的示例中使用了函数,它显示的错误是当我运行代码时HashTable没有被定义。我添加了jhashset.js,但仍然出错。 – Rizstien 2012-04-08 10:21:51

+1

问题解决了:) 我加了jhashtable。 – Rizstien 2012-04-08 11:12:02

+1

http://stackoverflow.com/questions/10062557/how-to-execute-javascipt-on-sproperty-struts-tag 你可以请看看这个 – Rizstien 2012-04-08 12:14:01

0

使用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)); 
+1

感谢Opaco的帮助,但我认为jQuery解决方案将更好地工作我的情况。 Konerak提供的解决方案给我的问题,“HashTable没有定义”,你可以请帮助吗? – Rizstien 2012-04-08 10:26:38

0
/** 
* 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, ','); 
};