【问题标题】:Javascript: Round up to the next multiple of 5Javascript:向上舍入到下一个 5 的倍数
【发布时间】:2013-09-23 06:57:41
【问题描述】:

我需要一个实用函数,它接收一个整数值(长度范围为 2 到 5 位),向上舍入到 5 的 下一个 倍数,而不是 最近的 5 的倍数。这是我得到的:

function round5(x)
{
    return (x % 5) >= 2.5 ? parseInt(x / 5) * 5 + 5 : parseInt(x / 5) * 5;
}

当我运行 round5(32) 时,它给了我 30,我想要 35。
当我运行round5(37) 时,它给了我35,我想要40。

当我运行 round5(132) 时,它给了我 130,我想要 135。
当我运行round5(137) 时,它给了我135,我想要140。

等等……

我该怎么做?

【问题讨论】:

  • round5(5) 应该给 5 还是 10?
  • 怎么样:将 x 除以 5,四舍五入到最接近的整数(使用 Math.ceil 函数),然后乘以 5?
  • round5(5) 应该给 5

标签: javascript math rounding


【解决方案1】:

这将完成工作:

function round5(x)
{
    return Math.ceil(x/5)*5;
}

这只是常见的舍入 number 到最接近的倍数 x 函数 Math.round(number/x)*x 的变体,但使用 .ceil 而不是 .round 使其始终根据数学规则向上舍入而不是向下/向上.

【讨论】:

  • 您能解释一下您是如何这么快就找到这个解决方案的吗?我认为 Math.ceil 只会将小数四舍五入为整数。
  • 嗯,这里确实是整数,@AmitErandole ;)
  • +1 表示紧凑和高效......它会四舍五入到 10,对吧? :)
  • 我会给这个函数添加另一个参数,表示“rounder”,所以原始数字可以四舍五入到我们在函数调用中设置的任何值,而不仅仅是固定 5...
  • 我喜欢这个解决方案!我用闭包实现了它,以便根据需要方便地更改多个内联:const roundToNearestMultipleOf = m => n => Math.round(n/m)*m 用法:roundToNearestMultipleOf(5)(32)
【解决方案2】:
const roundToNearest5 = x => Math.round(x/5)*5

这会将数字四舍五入到最接近的 5。要始终四舍五入到最接近的 5,请使用Math.ceil。同样,要始终向下舍入,请使用 Math.floor 而不是 Math.round。 然后,您可以像调用其他函数一样调用此函数。例如,

roundToNearest5(21)

将返回:

20

【讨论】:

  • +1 因为我需要一种方法来四舍五入到 最近的五个。 (OP 要求四舍五入到 next 五个(向上)。因此,接受的答案确实是正确的,@Oliver。)
【解决方案3】:

像这样?

function roundup5(x) { return (x%5)?x-x%5+5:x }

【讨论】:

  • 感谢您的解决方案,我的特殊情况需要一个不使用浮点数学但可以使用模数的汇总。
【解决方案4】:

我是在寻找类似的东西时来到这里的。 如果我的号码是 -0、-1、-2,它应该下降到 -0,如果是 -3、-4、-5,它应该下降到 -5。

我想出了这个解决方案:

function round(x) { return x%5<3 ? (x%5===0 ? x : Math.floor(x/5)*5) : Math.ceil(x/5)*5 }

还有测试:

for (var x=40; x<51; x++) {
  console.log(x+"=>", x%5<3 ? (x%5===0 ? x : Math.floor(x/5)*5) : Math.ceil(x/5)*5)
}
// 40 => 40
// 41 => 40
// 42 => 40
// 43 => 45
// 44 => 45
// 45 => 45
// 46 => 45
// 47 => 45
// 48 => 50
// 49 => 50
// 50 => 50

【讨论】:

  • 这可以通过使用Math.round更简单地完成
【解决方案5】:
voici 2 solutions possibles :
y= (x % 10==0) ? x : x-x%5 +5; //......... 15 => 20 ; 37 => 40 ;  41 => 45 ; 20 => 20 ; 

z= (x % 5==0) ? x : x-x%5 +5;  //......... 15 => 15 ; 37 => 40 ;  41 => 45 ; 20 => 20 ;

问候 保罗

【讨论】:

    【解决方案6】:

    // 精确舍入

    var round = function (value, precision) {
        return Math.round(value * Math.pow(10, precision)) / Math.pow(10, precision);
    };
    

    // 精确到 5 舍入

    var round5 = (value, precision) => {
        return round(value * 2, precision) / 2;
    }
    

    【讨论】:

      【解决方案7】:
      const fn = _num =>{
          return Math.round(_num)+ (5 -(Math.round(_num)%5))
      }
      

      使用round的原因是预期输入可以是随机数。

      谢谢!!!

      【讨论】:

        【解决方案8】:

        旧问题的新答案,没有if 也没有Math

        x % 5: the remainder
        5 - x % 5: the amount need to add up
        (5 - x % 5) % 5: make sure it less than 5
        x + (5 - x % 5) % 5: the result (x or next multiple of 5)
        
        ~~((x + N - 1) / N): equivalent to Math.ceil(x / N)
        

        function round5(x) {
         return x + (5 - x % 5) % 5;
        }
        
        function nearest_multiple_of(N, x) {
         return x + (N - x % N) % N;
        }
        
        function other_way_nearest_multiple_of(N, x) {
         return ~~((x + N - 1) / N) * N;
        }
        
        
        console.info(nearest_multiple_of(5,    0)); // 0
        console.info(nearest_multiple_of(5, 2022)); // 2025
        console.info(nearest_multiple_of(5, 2025)); // 2025
        
        console.info(other_way_nearest_multiple_of(5, 2022)); // 2025
        console.info(other_way_nearest_multiple_of(5, 2025)); // 2025

        【讨论】:

          【解决方案9】:
          function gradingStudents(grades) {
              // Write your code here
              const roundedGrades = grades.map(grade => {
                 if(grade >= 38 && grade %5 >=3){
                      while(grade % 5 != 0){
                          grade++
                      }
                  }
                  return grade
              }) 
              return roundedGrades
          }
          

          【讨论】:

          • 这似乎是在回答另一个问题。
          【解决方案10】:
          if( x % 5 == 0 ) {
              return int( Math.floor( x / 5 ) ) * 5;
          } else {
              return ( int( Math.floor( x / 5 ) ) * 5 ) + 5;
          }
          

          也许?

          【讨论】:

          • ReferenceError: int 未定义。也许你想要parseInt,但这不是必需的,因为Math.floor 返回一个数字。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多