【问题标题】:How to round up to the nearest 100 in JavaScript如何在 JavaScript 中四舍五入到最接近的 100
【发布时间】:2013-10-27 18:03:48
【问题描述】:

我想一直向上取整到最近的 100,无论值是 101 还是 199,它都应该向上取整到 200。例如:

var number = 1233;
//use something like Math.round() to round up to always 1300

我希望始终使用 jQuery 向上舍入到最接近的 100,而不是向下舍入。

【问题讨论】:

    标签: javascript math


    【解决方案1】:

    如果您想始终向上取整,请使用Math.ceil()

    Math.ceil(number/100)*100
    

    【讨论】:

      【解决方案2】:

      要将四舍五入到最接近的100,请使用Math.round

      Math.round(number/100)*100
      

      roundceil

      Math.round(60/100)*100 = 100Math.ceil(60/100)*100 = 100

      Math.round(40/100)*100 = 0Math.ceil(40/100)*100 = 100

      Math.round(-60/100)*100 = -100Math.ceil(-60/100)*100 = -0

      Math.round(-40/100)*100 = -0Math.ceil(-40/100)*100 = -0

      【讨论】:

        【解决方案3】:

        这一切都不需要 jQuery。只需使用 JavaScript 的Math.ceil

        Math.ceil(x / 100) * 100
        

        【讨论】:

          【解决方案4】:

          这是一个简单的方法:

          ((x/100).toFixed()*100;
          

          【讨论】:

            【解决方案5】:

            最简单的解决方案是:

            Math.round(number/100)*100
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-09-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-02-09
              相关资源
              最近更新 更多