Round money to nearest 10 dollars in Javascript

后端 未结 5 1425
不思量自难忘°
不思量自难忘° 2021-01-01 11:42

How can I round a decimal number in Javascript to the nearest 10? My math is pretty rubbish today, it could be the 2 hour sleep :/

Some sample cases

5条回答
  •  [愿得一人]
    2021-01-01 12:33

    function round(number, multiplier) {
      multiplier = multiplier || 1;
      return Math.round(number / multiplier) * multiplier;
    }
    
    var num1 = 2823.66;
    var num2 = 142.11;
    var num3 = 9.49;
    
    console.log(
      "%s\n%s\n%s", // just a formating thing
      round(num1, 10), // 2820
      round(num2, 10), // 140
      round(num3, 10)  // 10
    );
    

提交回复
热议问题