2015-06-15 103 views
0

我需要使用JavaScript在网页上显示格式化的数字。我想格式化,以便在正确的地方有逗号。我如何用正则表达式来做到这一点?我已经得到了这样的事情:格式货币正则表达式javascript

我想使用正则表达式使用JavaScript格式的货币为英语和法语。根据我的语言,它应该使用特定的正则表达式。 英语运作良好,但我想弄清楚法语。

return "$" + num.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,'); 

这是为英语工作。 因此,我的5000转换为$ 5,000.00,这很好。

我需要法国人看起来像这样5 000,00 $

+0

我不确定是否regex是正确的工具。但我想这个问题将有所帮助:http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript – Marvin

+0

http://stackoverflow.com/a/149099/ 4028085 – brso05

回答

1
return num = num.substring(1).replace(/,/g , " ").replace(".", ",") + " $"; 

使用此格式英语NUM。

+0

子字符串不工作 我使用了num.toFixed(2).replace(/,/ g,“”).replace(“。”,“,”)+“$”; 其工作范围扩展:例如: 使用正则表达式125900即将到达125900,00 $ 但我想要的东西像125 900,00 $ – Softwarex3