英文:
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(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
英文:
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({ // ....
英文:
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({ // ....
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论