使用简单的管道限制为2位小数

时间:2016-07-19 10:37:13

标签: angular pipe decimal

我找到了一个将数字限制为2位小数并将数字转换为货币数量的示例 - 例如£2.55。

{{ number | currency : 'GBP' : true : '1.2-2'}}

是否有一个简单的管道在不使用货币的情况下也能做同样的事情?

4 个答案:

答案 0 :(得分:148)

货币管道在内部使用number数字格式。所以你可以像这样使用它:

{{ number | number : '1.2-2'}}

答案 1 :(得分:1)

这是可行的

.ts -> pi = 3.1415

.html -> {{ pi | number : '1.0-2' }}

Ouput -> 3.14
  1. 如果它有一个小数,则只显示一个
  2. 如果有两个小数,则同时显示两个

https://stackblitz.com/edit/angular-e8g2pt?file=src/app/app.component.html

这对我有用!!!谢谢!

答案 2 :(得分:0)

现在角度5之后会有所不同:

{{ number | currency :'GBP':'symbol':'1.2-2' }}

答案 3 :(得分:0)

简单的解决方案

{{ orderTotal | number : '1.2-2'}}

//output like this

// public orderTotal = 220.45892221

//   {{ orderTotal | number : '1.2-2'}} 

// final Output
//  220.45
相关问题