Angular 角度2-计算出的数字有多圆?
我有一个*ngFor循环,我想计算一个值-小数点后2位 计算工作如下:Angular 角度2-计算出的数字有多圆?,angular,rounding,Angular,Rounding,我有一个*ngFor循环,我想计算一个值-小数点后2位 计算工作如下: {{date | amDifference:item.startdate:'minutes':true/item.duration*100}您需要另一个管道来执行此操作 import {Pipe} from 'angular2/core'; @Pipe({name: 'round'}) export class RoundPipe { transform (input:number) { return Mat
{{date | amDifference:item.startdate:'minutes':true/item.duration*100}您需要另一个管道来执行此操作
import {Pipe} from 'angular2/core';
@Pipe({name: 'round'})
export class RoundPipe {
transform (input:number) {
return Math.floor(input);
}
}
模板:
{{ date | amDifference : item.startdate : 'minutes' : true | round }}
如果其货币{value}货币:'INR':true:'1.0-0'}
如果只需要数字{{value}数字:'1.0-0'}
1.0-0表示:小数点前至少一位,小数点后0位
可以使用有角度的内置管道,例如 {{value}编号:'1.0-0'} 如果想要实现它:
@Pipe({name: 'round'})
export class RoundPipe {
transform (input:number) {
return Math.floor(input);
}
}
在模板中使用
{{1,1 | round}} => 1
{{2,1 | round}} => 2
{{11 | roundTen}} => 10
{{21 | roundTen}} => 20
另一个有用的管道是圆十:
@Pipe({name: 'roundTen'})
export class RoundTenPipe implements PipeTransform {
transform(value: number): number {
return Math.round(value / 10) * 10;
}
}
在模板中使用
{{1,1 | round}} => 1
{{2,1 | round}} => 2
{{11 | roundTen}} => 10
{{21 | roundTen}} => 20
非常感谢。我创建了一个round.pipe.ts并在app.module.ts import{pipe}中从'@angular/core'导入它;但是我得到一个错误,管道“圆形”无法找到。请确保您将其放入app.module.tsOh perfekt的声明中,这是错误的。很有效,很好。非常感谢你!地板不会围着我转。。。使用Math.round代替讨论问题这应该是公认的答案。谢谢