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

javascript - Round up to nearest - Stack Overflow

programmeradmin2浏览0评论

If the number is 37, I would like it to round to 40, if the number is 1086, I would like it to round to 2000. If the number is 453992, I would like it to round to 500000.

I don't really know how to describe this more generally, sorry, but basically, the number in the highest place should always round up to the nearest digit, and the rest bee zeros. I know how to round stuff normally, I just don't know how to cleanly deal with the variation among number of digits.

Thanks,

Edit: I deleted the 4 to 10 round, because that one seemed not to fit with the rest, and its not really necessary.

If the number is 37, I would like it to round to 40, if the number is 1086, I would like it to round to 2000. If the number is 453992, I would like it to round to 500000.

I don't really know how to describe this more generally, sorry, but basically, the number in the highest place should always round up to the nearest digit, and the rest bee zeros. I know how to round stuff normally, I just don't know how to cleanly deal with the variation among number of digits.

Thanks,

Edit: I deleted the 4 to 10 round, because that one seemed not to fit with the rest, and its not really necessary.

Share Improve this question edited Apr 9, 2013 at 15:36 dezman asked Apr 9, 2013 at 15:31 dezmandezman 19.4k12 gold badges57 silver badges92 bronze badges 4
  • One easy google showed this: developer.mozilla/en-US/docs/JavaScript/Reference/… – Henrik Andersson Commented Apr 9, 2013 at 15:40
  • 1 @limelights Your "easy google" doesn't answer the question. Math.round(37) = 37 – Curtis Commented Apr 9, 2013 at 15:52
  • 1 I believe if you read on the function for implementing rounding up to 10 is a bit further down in the examples, good sir. – Henrik Andersson Commented Apr 9, 2013 at 15:53
  • What if all the numbers after the first are already 0 like 4000? Would you want it to be 5000 or stay 4000? – markA Commented Apr 9, 2013 at 16:42
Add a ment  | 

6 Answers 6

Reset to default 8

Assuming all values are positive integers:

function roundUp(x){

    var y = Math.pow(10, x.toString().length-1);

    x = (x/y);
    x = Math.ceil(x);
    x = x*y;
    return x;
}

Live Demo: http://jsfiddle/fP7Z6/

I would use the following function

function specialRoundUp(num) {
    var factor = Math.pow(10, Math.floor(Math.log(num) / Math.LN10));
    return factor * Math.ceil(num/factor);
}

My fancy version which works correctly with decimals and negative numbers and correctly rounds to nearest power of ten as OP requested:

roundToNearestPow(value) {
    var digits = Math.ceil(Math.log10(Math.abs(value) + 1));
    var pow = Math.pow(10, digits - 1);

    return Math.round(value / pow) * pow;
}

// roundToNearestPow(-1499.12345) => 1000
// roundToNearestPow( 1500.12345) => 2000

Get the length of the original number:

var num;
var count = num.toString().length;

Get first number:

var first = num.toString().substring(0, 1);

Then just ++ first and add count-1 zeros

From Comments

Make sure number is not a product of 10:

if((num % 10) != 0)
{
    //do all above in this closure
}

I read it like nearest integer, which is production of an integer and some power of 10

You can get that by

var myCeil = function(num){
  var power = Math.log(num,10) * Math.LOG10E;
  var head = Math.floor(power);
  var rest = power - orig;
  return Math.ceil(Math.pow(10,next))*Math.pow(10,orig);
}
i = [37, 1086, 453992];
console.log( i.map(myCeil) );
// -> [ 40, 2000, 500000 ]

This should work with non-integer input as well.

If you would like to Round to the Nearest Power of 10, try this (javascript)

function Round2NearestPowerOf10(x) {
    x = Math.round(x);
    var strLen = x.toString().length;
    var y = x / Math.pow(10, strLen);
    var rst = Math.pow(10, strLen - 1 + Math.round(y));
    return rst < 10 ? 10 : rst;
}

Result will be rounded to 10,100,1000, etc..

发布评论

评论列表(0)

  1. 暂无评论