乐闻世界logo
搜索文章和话题

JS 如何将数字格式化为货币字符串

3个答案

1
2
3

要将数字格式化为货币字符串,我们通常会遵循以下步骤:

  1. 确定货币单位:首先要决定使用哪种货币单位,比如美元(USD)、欧元(EUR)等,因为每种货币的格式可能有所不同。

  2. 小数点精度:货币通常需要保留两位小数,即分为单位。

  3. 千分位分隔符:大数额通常会使用逗号(在某些国家为点)作为千分位分隔符。

  4. 货币符号:根据货币的不同,可能会在前面或后面添加货币符号,比如'$'表示美元。

  5. 负数的表示:如果数额是负的,可以通过括号或者负号来表示。

举个例子,如果我们要将数字1234567.89格式化为美元字符串,我们会这样做:

  • 确定货币单位:美元($)
  • 小数点精度:保留两位小数,即".89"
  • 千分位分隔符:使用逗号将数字分隔为千分位,即"1,234,567.89"
  • 货币符号:在数额前加上美元符号,即"$1,234,567.89"
  • 负数的表示:如果是负数,写作"-$1,234,567.89"或"($1,234,567.89)"

在编程中,这可以通过各种方式实现,例如在JavaScript中,我们可以使用Intl.NumberFormat对象来格式化货币:

javascript
const number = 1234567.89; const formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', }); const formatted = formatter.format(number); // "$1,234,567.89"

在Python中,我们可以使用内置的locale模块或者第三方库如Babel来实现相同的功能:

python
import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') number = 1234567.89 formatted = locale.currency(number, grouping=True) # "$1,234,567.89"

这些方法都可以实现将数字格式化为货币字符串的目的,并且可以根据不同的地区设置进行定制。

2024年6月29日 12:07 回复

简短而快速的解决方案(适用于任何地方!)

shell
(12345.67).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,'); // 12,345.67

该解决方案背后的想法是用第一个匹配项和逗号替换匹配的部分,即'$&,'。匹配是使用前瞻方法完成的。您可以将表达式理解为_“匹配一个数字,如果它后面跟着三个数字集(一个或多个)和一个点的序列”_。

测试:

shell
1 --> "1.00" 12 --> "12.00" 123 --> "123.00" 1234 --> "1,234.00" 12345 --> "12,345.00" 123456 --> "123,456.00" 1234567 --> "1,234,567.00" 12345.67 --> "12,345.67"

演示: http: //jsfiddle.net/hAfMM/9571/


扩展短解

您还可以扩展对象的原型Number以添加对任意小数位数[0 .. n]和数字组大小的额外支持[0 .. x]

shell
/** * Number.prototype.format(n, x) * * @param integer n: length of decimal * @param integer x: length of sections */ Number.prototype.format = function(n, x) { var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\.' : '$') + ')'; return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&,'); }; 1234..format(); // "1,234" 12345..format(2); // "12,345.00" 123456.7.format(3, 2); // "12,34,56.700" 123456.789.format(2, 4); // "12,3456.79"

演示/测试: http://jsfiddle.net/hAfMM/435/


超长短解

在此超级扩展版本中您可以设置不同的分隔符类型:

shell
/** * Number.prototype.format(n, x, s, c) * * @param integer n: length of decimal * @param integer x: length of whole part * @param mixed s: sections delimiter * @param mixed c: decimal delimiter */ Number.prototype.format = function(n, x, s, c) { var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')', num = this.toFixed(Math.max(0, ~~n)); return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ',')); }; 12345678.9.format(2, 3, '.', ','); // "12.345.678,90" 123456.789.format(4, 4, ' ', ':'); // "12 3456:7890" 12345678.9.format(0, 3, '-'); // "12-345-679"

演示/测试: http://jsfiddle.net/hAfMM/612/

2024年6月29日 12:07 回复

看一下 JavaScript Number对象,看看它是否可以帮助您。

  • toLocaleString()将使用特定于位置的千位分隔符格式化数字。
  • toFixed()将数字四舍五入到特定的小数位数。

要同时使用这些值,必须将其类型更改回数字,因为它们都输出字符串。

例子:

shell
Number((someNumber).toFixed(1)).toLocaleString()

编辑

可以直接使用 toLocaleString ,不需要重新转换为数字:

shell
someNumber.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2});

多个号码

如果您需要经常类似地格式化数字,您可以创建一个特定的对象以供重用。就像德语(瑞士):

shell
const money = new Intl.NumberFormat('de-CH', { style:'currency', currency: 'CHF' }); const percent = new Intl.NumberFormat('de-CH', { style:'percent', maximumFractionDigits: 1, signDisplay: "always"});

可以用作:

shell
money.format(1234.50); // output CHF 1'234.50 percent.format(0.083); // output +8.3%

相当漂亮。

2024年6月29日 12:07 回复

你的答案