如何在Javascript中找到当前区域设置的小数分隔符

时间:2021-10-20 07:29:49

In my application there are several places where the user is expected to enter a monetary value. The application supports storing these monetary values in multiple currencies as well as localization.

在我的应用程序中,有几个地方要求用户输入货币值。该应用程序支持以多种货币存储这些货币值以及本地化。

I'm using the Intl.NumberFormat object in Javascript to format a number into any currency and locale I need, but there's no way to unformat the number using that.

我在Javascript中使用Intl.NumberFormat对象将数字格式化为我需要的任何货币和区域设置,但是没有办法使用它来格式化数字。

All of the libraries I've found so far require you to provide the decimal and thousands separators in order for the formatting/unformatting functions to work. However, I need a way to find what the decimal and thousands separators are in the first place so I can't use those.

到目前为止,我发现的所有库都要求您提供十进制和千位分隔符,以便格式化/取消格式化功能起作用。但是,我需要一种方法来找到小数和千位分隔符的位置,所以我不能使用它们。

How can I reliably get the decimal separator for the current locale?

如何可靠地获取当前区域设置的小数分隔符?

1 个解决方案

#1


0  

Option 1: using Intl.NumberFormat#formatToParts

选项1:使用Intl.NumberFormat #formatToParts

Most reliable method, only works for browsers supporting the Intl API. Otherwise it requires an Intl polyfill

最可靠的方法,仅适用于支持Intl API的浏览器。否则它需要Intl polyfill

function getDecimalSeparator(locale) {
    const numberWithDecimalSeparator = 1.1;
    return Intl.NumberFormat(locale)
        .formatToParts(numberWithDecimalSeparator)
        .find(part => part.type === 'decimal')
        .value;
}

Option 2: using toLocaleString

选项2:使用toLocaleString

Less elegant, it relies on the fact that a separator is always one character long, which seems to be the case for all languages: Decimal separator - Wikipedia

不太优雅,它依赖于一个分隔符总是一个字符长的事实,这似乎是所有语言的情况:十进制分隔符 - *

function getDecimalSeparator(locale) {
    const numberWithDecimalSeparator = 1.1;

    return numberWithDecimalSeparator
        .toLocaleString(locale)
        .substring(1, 2);
}

This has been suggested here: With a browser, how do I know which decimal separator that the client is using?

这里有人建议:使用浏览器,我如何知道客户端使用哪个小数分隔符?

Examples:

> getDecimalSeparator()
"."
> getDecimalSeparator('fr-FR')
","

Bonus of option 1:

选项1的奖金:

We could extend it to retrieve either the decimal or group separator of a given locale:

我们可以扩展它以检索给定语言环境的小数或组分隔符:

function getSeparator(locale, separatorType) {
        const numberWithGroupAndDecimalSeparator = 1000.1;
        return Intl.NumberFormat(locale)
            .formatToParts(numberWithGroupAndDecimalSeparator)
            .find(part => part.type === separatorType)
            .value;
    }

Examples:

> getSeparator('en-US', 'decimal')
"."
> getSeparator('en-US', 'group')
","
> getSeparator('fr-FR', 'decimal')
","
> getSeparator('fr-FR', 'group')
" "

#1


0  

Option 1: using Intl.NumberFormat#formatToParts

选项1:使用Intl.NumberFormat #formatToParts

Most reliable method, only works for browsers supporting the Intl API. Otherwise it requires an Intl polyfill

最可靠的方法,仅适用于支持Intl API的浏览器。否则它需要Intl polyfill

function getDecimalSeparator(locale) {
    const numberWithDecimalSeparator = 1.1;
    return Intl.NumberFormat(locale)
        .formatToParts(numberWithDecimalSeparator)
        .find(part => part.type === 'decimal')
        .value;
}

Option 2: using toLocaleString

选项2:使用toLocaleString

Less elegant, it relies on the fact that a separator is always one character long, which seems to be the case for all languages: Decimal separator - Wikipedia

不太优雅,它依赖于一个分隔符总是一个字符长的事实,这似乎是所有语言的情况:十进制分隔符 - *

function getDecimalSeparator(locale) {
    const numberWithDecimalSeparator = 1.1;

    return numberWithDecimalSeparator
        .toLocaleString(locale)
        .substring(1, 2);
}

This has been suggested here: With a browser, how do I know which decimal separator that the client is using?

这里有人建议:使用浏览器,我如何知道客户端使用哪个小数分隔符?

Examples:

> getDecimalSeparator()
"."
> getDecimalSeparator('fr-FR')
","

Bonus of option 1:

选项1的奖金:

We could extend it to retrieve either the decimal or group separator of a given locale:

我们可以扩展它以检索给定语言环境的小数或组分隔符:

function getSeparator(locale, separatorType) {
        const numberWithGroupAndDecimalSeparator = 1000.1;
        return Intl.NumberFormat(locale)
            .formatToParts(numberWithGroupAndDecimalSeparator)
            .find(part => part.type === separatorType)
            .value;
    }

Examples:

> getSeparator('en-US', 'decimal')
"."
> getSeparator('en-US', 'group')
","
> getSeparator('fr-FR', 'decimal')
","
> getSeparator('fr-FR', 'group')
" "