如何使用货币管道获取组件中最多两位小数的值?

新手上路,请多包涵

我有一个值,如 54781.7622000、1123.11。我希望这个值的格式类似于 \(54781.76、\)1123.11。

 //import currency pipe
import { CurrencyPipe } from '@angular/common'

//initialize  in constructor
constructor(private cp: CurrencyPipe)

this.formatedOutputValue = this.cp.transform(parseInt(this.outputValue));

我试过在这个值之后求和额外的参数。

 this.formatedOutputValue = this.cp.transform(parseInt(this.outputValue),1.2-2);

但是不锻炼。

原文由 swapnil jain 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 208
1 个回答

您没有将所有需要的参数传递给 transform()

这是货币管道 transform() 在 Angular 中接受的内容(来源: Angular 代码库

 transform(value: any, currencyCode?: string, display: 'code'|'symbol'|'symbol-narrow'|string|boolean = 'symbol', digitsInfo?: string, locale?: string)

您可以通过将正确的数据传递给 transform() 来解决您的问题,如下所示。

 this.formatedOutputValue = this.cp.transform(this.outputValue, 'USD', 'symbol', '1.2-2');

这是 StackBlitz 上的一个工作示例。您还可以在本例中看到如何在模板中直接使用管道。

原文由 nash11 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题