admin管理员组

文章数量:1289517

In English locale the number looks like this: 111,111,222.00 so thousand separator is a ma 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 getLocaleNumberFormat() function in angular, but I couldn't find how to use it as it always return format en locale format

In English locale the number looks like this: 111,111,222.00 so thousand separator is a ma 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/mon/getLocaleNumberFormat getLocaleNumberFormat() function in angular, but I couldn't find how to use it as it always return format en locale format

Share Improve this question edited Jan 6, 2020 at 9:41 user122222 asked Jan 6, 2020 at 6:49 user122222user122222 2,4496 gold badges46 silver badges88 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Use getLocaleNumberSymbol().

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

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

// returns thousands sepator, ma (,) 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/mon/locales/global/en-GB';

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

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

I usually set global locale in my app.module.

App.Module.ts

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

registerLocaleData(localeHe, 'he');

@NgModule({ // ....

Angular I18n docs

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

getLocaleNumberFormat(locale: string, type: NumberFormatStyle): string

Parameters:
locale : string A locale code for the locale format rules to use.
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.

本文标签: javascriptGet thousand separator of numbers in different languages AngularStack Overflow