最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

math - How to round up number to nearest 1001000 depending on number, in JavaScript? - Stack Overflow

programmeradmin15浏览0评论

I have a number that can be in the 2 digits, like 67, 24, 82, or in the 3 digits, like 556, 955, 865, or 4 digits and so on. How can I round up the number to the nearest n+1 digits depending on the number?

Example:

roundup(87) => 100,
roundup(776) => 1000,
roudnup(2333) => 10000

and so on.

I have a number that can be in the 2 digits, like 67, 24, 82, or in the 3 digits, like 556, 955, 865, or 4 digits and so on. How can I round up the number to the nearest n+1 digits depending on the number?

Example:

roundup(87) => 100,
roundup(776) => 1000,
roudnup(2333) => 10000

and so on.

Share Improve this question edited Aug 12, 2022 at 5:07 user2357112 280k31 gold badges478 silver badges558 bronze badges asked Jul 4, 2018 at 8:47 mikasamikasa 9003 gold badges13 silver badges33 bronze badges 4
  • Possible duplicate of Rounding to the closest 100 – Elydasian Commented Jul 4, 2018 at 9:07
  • what is the desired behavior for 10 itself? 10 or 100? The answers below differ on that. – Jeremy Kahan Commented Jul 4, 2018 at 13:18
  • @JeremyKahan its roundup so the answer should be 100. – mikasa Commented Jul 4, 2018 at 19:16
  • So just know the first version of the accepted answer gives 10 when you feed it 10 (the second gives 100) – Jeremy Kahan Commented Jul 4, 2018 at 22:06
Add a comment  | 

5 Answers 5

Reset to default 18

You could take the logarithm of ten and round up the value for getting the value.

function roundup(v) {
    return Math.pow(10, Math.ceil(Math.log10(v)));
}

console.log(roundup(87));   //   100
console.log(roundup(776));  //  1000
console.log(roundup(2333)); // 10000

For negative numbers, you might save the sign by taking the result of the check as factor or take a negative one. Then an absolute value is necessary, because logarithm works only with positive numbers.

function roundup(v) {
    return (v >= 0 || -1) * Math.pow(10, 1 + Math.floor(Math.log10(Math.abs(v))));
}

console.log(roundup(87));    //    100
console.log(roundup(-87));   //   -100
console.log(roundup(776));   //   1000
console.log(roundup(-776));  //  -1000
console.log(roundup(2333));  //  10000
console.log(roundup(-2333)); // -10000

 const roundup = n => 10 ** ("" + n).length

Just use the number of characters.

You can check how many digits are in the number and use exponentiation:

const roundup = num => 10 ** String(num).length;
console.log(roundup(87));
console.log(roundup(776));
console.log(roundup(2333));

You can use String#repeat combined with Number#toString in order to achieve that :

const roundUp = number => +('1'+'0'.repeat(number.toString().length));

console.log(roundUp(30));
console.log(roundUp(300));
console.log(roundUp(3000));

//Math.pow(10,(value+"").length)   


console.log(Math.pow(10,(525+"").length))
console.log(Math.pow(10,(5255+"").length))

I came up with another solution that does'nt require a new function to be created

发布评论

评论列表(0)

  1. 暂无评论