获取不同语言中的数字千位分隔符 Angular

huangapple go评论170阅读模式
英文:

Get thousand separator of numbers in different languages Angular

问题

在英文区域设置下,数字看起来像这样:111,111,222.00,所以千位分隔符是逗号,小数点分隔符是点号。例如,在德国,相同的数字看起来像111.111.222,00,所以千位和小数点分隔符被颠倒了。有没有一种方法可以根据区域设置找到千位分隔符?

我在Angular中找到了https://angular.io/api/common/getLocaleNumberFormat getLocaleNumberFormat()函数,但我找不到如何使用它,因为它总是返回en区域的格式。

英文:

In English locale the number looks like this: 111,111,222.00 so thousand separator is a comma and decimal separator is a point. In e.g. German the same number looks like 111.111.222,00 so thousand and decimal separator are reversed. Is there a way to find a thousand separator based on locale?

I found https://angular.io/api/common/getLocaleNumberFormat getLocaleNumberFormat() function in angular, but I couldn't find how to use it as it always return format en locale format

答案1

得分: 6

使用 getLocaleNumberSymbol()

getLocaleNumberSymbol(locale: string, symbol: NumberSymbol): string

import { getLocaleNumberSymbol, NumberSymbol } from '@angular/common';

// 返回千位分隔符,在此示例中为逗号 (,):
getLocaleNumberSymbol('en-US', NumberSymbol.CurrencyGroup)
// 返回小数点分隔符,在此示例中为句点 (.):
getLocaleNumberSymbol('en-US', NumberSymbol.CurrencyDecimal)

如果您的区域设置不同于 en-US,则应将区域数据导入到 AppModule 中:

import '@angular/common/locales/global/en-GB';

@NgModule({...})
export class AppModule {
}

可用区域设置的列表可以在此处找到:
https://github.com/angular/angular/tree/master/packages/common/locales

英文:

Use getLocaleNumberSymbol().

getLocaleNumberSymbol(locale: string, symbol: NumberSymbol): string

import { getLocaleNumberSymbol, NumberSymbol } from '@angular/common';

// returns thousands sepator, comma (,) in this case:
getLocaleNumberSymbol('en-US', NumberSymbol.CurrencyGroup)
// returns decimal separator, dot (.) in this case:
getLocaleNumberSymbol('en-US', NumberSymbol.CurrencyDecimal)

You should import your locale data into the AppModule if it's different from en-US:

import '@angular/common/locales/global/en-GB';

@NgModule({...})
export class AppModule {
}

List of available locales can be found here:
https://github.com/angular/angular/tree/master/packages/common/locales

答案2

得分: 1

我通常在我的app.module中设置全局区域设置。

App.Module.ts

import { registerLocaleData } from '@angular/common';
import localeHe from '@angular/common/locales/he';

registerLocaleData(localeHe, 'he');

@NgModule({ // ....

Angular国际化文档

英文:

I usually set global locale in my app.module.

App.Module.ts

import { registerLocaleData } from '@angular/common';
import localeHe from '@angular/common/locales/he';

registerLocaleData(localeHe, 'he');

@NgModule({ // ....

Angular I18n docs

答案3

得分: 1

根据Angular文档的说法,您必须将区域设置作为getLocaleNumberFormat()函数的第一个参数提供:

> getLocaleNumberFormat(locale: string, type: NumberFormatStyle): string<br />
> <br />
> 参数:<br />
> locale : string 用于使用区域设置格式规则的区域设置代码。<br />
> type : NumberFormatStyle 要格式化的数值类型(如十进制或货币)。

如果您提供正确的语言字符串,格式化应该可以正常工作。

英文:

As the Angular documentation says, you have to provide the locale as first argument to the getLocaleNumberFormat() function:

> getLocaleNumberFormat(locale: string, type: NumberFormatStyle): string<br />
> <br />
> Parameters:<br />
> locale : string A locale code for the locale format rules to use. <br />
> type : NumberFormatStyle The type of numeric value to be formatted (such as Decimal or Currency.)

If you provide the correct language string, the formatting should work.

huangapple
  • 本文由 发表于 2020年1月6日 14:49:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定