【问题标题】:Angular - Limit decimal to two places without rounding offAngular - 将小数限制为两位而不四舍五入
【发布时间】:2019-07-19 21:22:24
【问题描述】:

我正在使用decimal pipe 将十进制数限制为两个位置:

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

但不幸的是,上面的管道使数字四舍五入。有什么办法可以将输出限制在小数点后两位而不四舍五入。

【问题讨论】:

    标签: angular


    【解决方案1】:

    数字管道将对数字进行四舍五入。 您可以使用我的公式并创建自定义管道。 显示 2digit 的示例。

    let number = 1236.25658
    let customDigit = parseInt(number * 100) / 100;
    console.log(customDigit) //return 1236.25
    

    【讨论】:

      【解决方案2】:

      我建议您编写一个自定义管道来执行此操作。

      文档可在此处找到:

      https://angular.io/guide/pipes#custom-pipes

      这是一个很好的例子,如何使用管道来格式化数字

      Angular 2 - Number pipe for scientific notation numbers

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-09
        • 1970-01-01
        • 1970-01-01
        • 2011-05-10
        • 1970-01-01
        • 2014-05-09
        • 2023-03-24
        相关资源
        最近更新 更多