我正在尝试将从API中获得的金额格式化为特定于语言环境的格式。
在控制台中:
Intl.NumberFormat('en-IN').format(450000) // "4,50,000"
在Angular 2组件模板中:
{{ Intl.NumberFormat('en-IN').format(450000) }} // Cannot read property 'NumberFormat' of undefined
{{ 450000 | currency:'INR':true }} // ₹450,000.00
有没有一种方法可以获取
₹4,50,000.00
而不显式指定定界符(并且可以将语言环境字符串'en-IN'
更改为'en-US'
进行更新)。 最佳答案
只需使用货币管道即可简化操作。假设模板中的变量称为myamount
。然后使用
@Component({
selector: 'currency-pipe',
template: `<div>
<p>Amount: {{myamount | currency:'USD':true}}</p>
</div>`
})
export class CurrencyPipeComponent {
myamount: number = 0.259;
}
有关货币管道的来源和更多信息:https://angular.io/docs/ts/latest/api/common/index/CurrencyPipe-pipe.html
您可能还需要设置默认区域性:How to set locale in DatePipe in Angular2?
在食谱中,您可能会找到有关i18n以及如何在运行时进行更改的更多信息:
<script>
// Get the locale id somehow
document.locale = 'fr';
// Map to the text plugin
System.config({
map: {
text: 'systemjs-text-plugin.js'
}
});
// Launch the app
System.import('app').catch(function(err){ console.error(err); });
</script>
资料来源:https://angular.io/docs/ts/latest/cookbook/i18n.html#!#merge-with-the-jit-compiler
关于angular - 货币编号格式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40506655/