javascript - 如何使用货币管道在组件中获取最多 2 位十进制数字的值?
问题描述
我有一个像 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);
但不锻炼。
解决方案
您没有将所有需要的参数传递给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上的一个工作示例。您还可以在此示例中看到如何直接使用模板中的管道。
推荐阅读
- dataframe - 按创建日期获取不同的行
- python-3.x - Python:获取文本的美丽汤
- javascript - 如何仅显示前 4 个 HTML 元素并隐藏其余元素?
- authentication - 微服务之间的横向认证方法(RESTful)
- javascript - 使用javascript滚动水平溢出div中心
- gcloud - Gcloud ai-platform,无法使用自己的预测类创建模型
- bash - 在给定搜索字符串的情况下获取多行字符串的后续问题
- signalr - 服务器端 Blazor 能否造成渲染延迟?
- laravel-5 - 在 laravel 5.8 中创建自定义缓存驱动器给出错误“不支持驱动程序 [aerospike]。”
- vuejs2 - 将 lang="less" 添加到 component.vue 中的样式标记后找不到模块