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

rus*_*i88 74 pipe decimal angular

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

{{ number | currency : 'GBP' : true : '1.2-2'}}
Run Code Online (Sandbox Code Playgroud)

是否有一个简单的管道在不使用货币的情况下也能做到这一点?

dfs*_*fsq 176

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

{{ number | number : '1.2-2'}}
Run Code Online (Sandbox Code Playgroud)

  • 对于未来的读者,`{{x | number:'1.2'}}`也有效,意思相同. (16认同)
  • 这有助于理解为什么`1.2-2`/sf/ask/2693457931/ (8认同)
  • mehaase,是不一样的.例如,如果您有5.6和5.6789,则输出将为5.60和5.6789.因此,第一个参数是min,第二个参数是最大位数. (5认同)
  • [参考](https://angular.io/docs/ts/latest/api/common/index/DecimalPipe-pipe.html)有关使用数字管道的详细信息 (3认同)
  • 如何使用 Pipe 将数字转换为小数点后 1 位且不进行四舍五入。例如:345.678 => 345.6 (2认同)

小智 15

其作品

.ts -> pi = 3.1415

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

Ouput -> 3.14
Run Code Online (Sandbox Code Playgroud)
  1. 如果有小数则只显示一位
  2. 如果有两位小数,则显示两者

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

这对我有用!谢谢!!


pab*_*oRN 7

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

{{ number | currency :'GBP':'symbol':'1.2-2' }}
Run Code Online (Sandbox Code Playgroud)


Sha*_*pta 7

简单的解决方案

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

//output like this

// public orderTotal = 220.45892221

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

// final Output
//  220.45
Run Code Online (Sandbox Code Playgroud)