javascript - 如何在 JavaScript 中使用 localeString 获取货币代码和金额之间的空格?

标签 javascript localization currency

我必须使用基于国家/地区的逗号和句点分隔符的国家/地区代码以货币格式显示数字。

例如如果数字是 4294967295000 那么

  1. 美国 = 4,294,967,295,000.00 美元
  2. 印度 = 42,94,96,72,95,000.00 印度卢比

我在印度使用它,但在美国我得到这个字符串,但我需要在货币代码和数字之间留一个空格:

var number = 4294967295000;

console.log(number.toLocaleString('en-IN', {
  style: 'currency', currency: 'INR', currencyDisplay: 'code'
}));     // INR 42,94,96,72,95,000.00

console.log(number.toLocaleString('en-US', {
  style: 'currency', currency: 'USD', currencyDisplay: 'code'
}));     // USD4,294,967,295,000.00

如何实现“USD”和数字之间的间距?我没有在有关空间的选项参数中看到任何内容。我可以编写自定义代码来增加空间,但我想看看是否有更好的选择来做同样的事情。

最佳答案

I did not see anyting in option parameter regarding space.

所以我出发了。

当您将选项传递给 toLocaleString 时,它会遵循许多步骤。首先,它将您传入的选项转换为 NumberFormat 对象。它goes through a series of steps to do so ,其中之一是:

  1. If s is "currency", then
    a. Let c be converting c to upper case as specified in 6.1.
    b. Set numberFormat.[[currency]] to c.

这意味着无论您作为 currency 选项传入什么,只要它是正确的货币代码,都将转换为大写并存储在内部 currency NumberFormat 对象的属性。

然后我们看到还有一些其他的 internal propertiesNumberFormat 上使用 - 在这种情况下,特别是 positivePattern 内部插槽。规范说明:

The value of these properties must be string values that contain a substring "{number}"; the values within the currency property must also contain a substring "{currency}". The pattern strings must not contain any characters in the General Category “Number, decimal digit" as specified by the Unicode Standard.

IE 请注意,此时,对于给定的文化,我们已经创建了一个对象,该对象实际上具有沿 {currency} {number} 行的格式字符串。只有在 Chrome 的(至少)美元情况下,它是 {currency}{number}。请注意,在 IE 和 Edge 中,您在 USD 之后获得空格,因此它由 {currency} {number} 的格式字符串决定。

接下来,我们进入 actual implementation of formatting the number .第 7 步说:

  1. If the value of the numberFormat.[[style]] is "currency", then
    a. Let currency be the value of numberFormat.[[currency]].
    b. If numberFormat.[[currencyDisplay]] is "code", then
    i. Let cd be currency.
    c. Else, if numberFormat.[[currencyDisplay]] is "symbol", then
    i. Let cd be an ILD string representing currency in short form. If the implementation does not have such a representation of currency, then use currency itself.
    d. Else, if numberFormat.[[currencyDisplay]] is "name", then
    i. Let cd be an ILD string representing currency in long form. If the implementation does not have such a representation of currency, then use currency itself.
    e. Replace the substring "{currency}" within result with cd.

强调我的,显示在这种情况下采取的步骤。


TL;DR - 此行为似乎取决于浏览器,如果您始终需要空间,则需要自己解析和修复结果字符串,没有内置方法可以这样做.

关于javascript - 如何在 JavaScript 中使用 localeString 获取货币代码和金额之间的空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49325561/

相关文章:

java - 以两种不同的货币显示相同的交易(金额/票价)

ios - 如何将 UILabel 的输出格式化为货币?

javascript - 顺序的 setState 调用在时间上非常接近,只会引发一次渲染

javascript - Angular 2 : many Async pipes vs one subscribe

javascript - 需要对带有选项卡导航(分页)的猫头鹰幻灯片进行 jquery 和 litte html css 改进

php - 如何国际化 PHP 第三方库

javascript - 属性 'locale' 的 GWT I18N javascript 问题

C++ 程序 - "Making Change"应用程序产生奇怪的结果

javascript - anchor 标记仅适用于 Firefox

c# - 如何以独立于区域设置的方式定位本地组?