gpt4 book ai didi

javascript - 获取货币符号 Angular 2

转载 作者:数据小太阳 更新时间:2023-10-29 05:03:28 26 4
gpt4 key购买 nike

我正在使用 Angular 2 和货币管道构建应用程序,但我找不到根据没有任何数字的 ISO 值获取货币符号的方法。我的意思是我只想要符号而不设置要格式化的数字。

正常情况 $3.00我只需要 $symbol,不需要数字

最佳答案

Angular 提供了一个内置方法 getCurrencySymbol,它可以为您提供货币符号。您可以将管道作为该方法的包装器编写为

import { Pipe, PipeTransform } from '@angular/core';
import { getCurrencySymbol } from '@angular/common';

@Pipe({
name: 'currencySymbol'
})
export class CurrencySymbolPipe implements PipeTransform {

transform(
code: string,
format: 'wide' | 'narrow' = 'narrow',
locale?: string
): any {
return getCurrencySymbol(code, format, locale);
}
}

然后用作:

{{'USD'| currencySymbol}} ===> $
{{'INR'| currencySymbol}} ===> ₹

现场 Stackblitz 演示:

应用程序网址:https://angular-currency-symbol-pipe.stackblitz.io

编辑器网址:https://stackblitz.com/edit/angular-currency-symbol-pipe

关于javascript - 获取货币符号 Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46077521/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com