JavaScript tour vers le plus proche 10
var rounded = Math.round(number / 10) * 10
TC5550
var rounded = Math.round(number / 10) * 10
/*
Gets the nearest multiple of a given number.
@param {Number} number The number to go up from.
@param {Number} multiple The base to get the multiple of.
*/
function getNearestMultiple(number, multiple)
{
return number + (multiple - (number % multiple));
}
/* example code below (very repetitive, but you don't need to copy it) */
var num1 = 1;
var num2 = 4;
var num3 = 6;
var num4 = 8;
var num5 = 13;
var num6 = 12;
var num7 = 14;
var num8 = 19;
var num9 = 21;
console.log(`\nNumber: ${num1}\nBase: 2\nNext Multiple: ${getNearestMultiple(num1, 2)}`);
console.log(`\nNumber: ${num2}\nBase: 3\nNext Multiple: ${getNearestMultiple(num2, 3)}`);
console.log(`\nNumber: ${num3}\nBase: 4\nNext Multiple: ${getNearestMultiple(num3, 4)}`);
console.log(`\nNumber: ${num4}\nBase: 5\nNext Multiple: ${getNearestMultiple(num4, 5)}`);
console.log(`\nNumber: ${num5}\nBase: 6\nNext Multiple: ${getNearestMultiple(num5, 6)}`);
console.log(`\nNumber: ${num6}\nBase: 7\nNext Multiple: ${getNearestMultiple(num6, 7)}`);
console.log(`\nNumber: ${num7}\nBase: 8\nNext Multiple: ${getNearestMultiple(num7, 8)}`);
console.log(`\nNumber: ${num8}\nBase: 9\nNext Multiple: ${getNearestMultiple(num8, 9)}`);
console.log(`\nNumber: ${num9}\nBase: 10\nNext Multiple: ${getNearestMultiple(num9, 10)}`);
// output
/*
Number: 1
Base: 2
Next Multiple: 2
Number: 4
Base: 3
Next Multiple: 6
Number: 6
Base: 4
Next Multiple: 8
Number: 8
Base: 5
Next Multiple: 10
Number: 13
Base: 6
Next Multiple: 18
Number: 12
Base: 7
Next Multiple: 14
Number: 14
Base: 8
Next Multiple: 16
Number: 19
Base: 9
Next Multiple: 27
Number: 21
Base: 10
Next Multiple: 30
*/