Angular 2 中的小数管 - 没有四舍五入的小数位

Decimal Pipe in Angular 2 - No Decimal Places WITHOUT Rounding

我目前使用这个管道 {{ person.ageInDays/7 | number:'1.0-0' }} 来显示一个人的周龄,但似乎工作不准确!它有时会将数字四舍五入。

例如,对于 257 天大的婴儿,它应该是 36 周零 5 天,而结果是 37 周零 5 天!

如果你看到angular doc,它清楚地提到了数字管道的行为。

If no parameters are specified, the function rounds off to the nearest value using this rounding method. The behavior differs from that of the JavaScript Math.round() function. In the following case for example, the pipe rounds down where Math.round() rounds up

您必须根据自己的情况创建自定义管道

@Pipe({name: 'daystoweek'})
export class DaysToWeekPipe implements PipeTransform {
    transform(value: number): number {
        return Math.floor(value);
    }
}

在您的代码中使用该自定义管道

{{ numbervalue | daystoweek}}