Math.ceil 到位置 1 处最接近的五位

发布于 2024-12-04 12:15:51 字数 212 浏览 2 评论 0原文

好吧......

我有很多不受控制的数字想要四舍五入:

51255 -> 55000
25 -> 25
9214 -> 9500
13135 -> 15000
25123 -> 30000

我尝试将数字修改为字符串并计算长度......

但是是否有一种简单的方法使用一些数学函数?

Okay....

I have a lot of uncontrolled numbers i want to round:

51255 -> 55000
25 -> 25
9214 -> 9500
13135 -> 15000
25123 -> 30000

I have tried modifying the numbers as string and counting length....

But is there a simple way using some Math function maybe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风铃鹿 2024-12-11 12:15:51

这是我迟来的答案。不使用 Math 方法。

function toN5( x ) {
    var i = 5;
    while( x >= 100 ) {x/=10; i*=10;}
    return ((~~(x/5))+(x%5?1:0)) * i;
}

演示: http://jsbin.com/ujamoj/edit#javascript, live

   [51255, 24, 25, 26, 9214, 13135, 25123, 1, 9, 0].map( toN5 );

// [55000, 25, 25, 30, 9500, 15000, 30000, 5, 10, 0]

或者这可能更干净一点:

function toN5( x ) {
    var i = 1;
    while( x >= 100 ) {x/=10; i*=10;}
    return (x + (5-((x%5)||5))) * i;
}

DEMO: http://jsbin.com/idowan/edit#javascript,live

要分解它:

function toN5( x ) {
   //       v---we're going to reduce x to the tens place, and for each place
   //       v       reduction, we'll multiply i * 10 to restore x later.
    var i = 1;

   // as long as x >= 100, divide x by 10, and multiply i by 10.
    while( x >= 100 ) {x/=10; i*=10;}

   // Now round up to the next 5 by adding to x the difference between 5 and
   //    the remainder of x/5 (or if the remainder was 0, we substitute 5
   //    for the remainder, so it is (x + (5 - 5)), which of course equals x).

   // So then since we are now in either the tens or ones place, and we've
   //    rounded to the next 5 (or stayed the same), we multiply by i to restore
   //    x to its original place.
    return (x + (5-((x%5)||5))) * i;
}

或者为了避免逻辑运算符,而只使用算术运算符,我们可以这样做:

return (x + ((5-(x%5))%5)) * i;

并传播它出一点:

function toN5( x ) {
    var i = 1;
    while( x >= 100 ) {
        x/=10; 
        i*=10;
    }
    var remainder = x % 5;
    var distance_to_5 = (5 - remainder) % 5;
    return (x + distance_to_5) * i;
}

Here's my late answer. Uses no Math methods.

function toN5( x ) {
    var i = 5;
    while( x >= 100 ) {x/=10; i*=10;}
    return ((~~(x/5))+(x%5?1:0)) * i;
}

DEMO: http://jsbin.com/ujamoj/edit#javascript,live

   [51255, 24, 25, 26, 9214, 13135, 25123, 1, 9, 0].map( toN5 );

// [55000, 25, 25, 30, 9500, 15000, 30000, 5, 10, 0]

Or this is perhaps a bit cleaner:

function toN5( x ) {
    var i = 1;
    while( x >= 100 ) {x/=10; i*=10;}
    return (x + (5-((x%5)||5))) * i;
}

DEMO: http://jsbin.com/idowan/edit#javascript,live

To break it down:

function toN5( x ) {
   //       v---we're going to reduce x to the tens place, and for each place
   //       v       reduction, we'll multiply i * 10 to restore x later.
    var i = 1;

   // as long as x >= 100, divide x by 10, and multiply i by 10.
    while( x >= 100 ) {x/=10; i*=10;}

   // Now round up to the next 5 by adding to x the difference between 5 and
   //    the remainder of x/5 (or if the remainder was 0, we substitute 5
   //    for the remainder, so it is (x + (5 - 5)), which of course equals x).

   // So then since we are now in either the tens or ones place, and we've
   //    rounded to the next 5 (or stayed the same), we multiply by i to restore
   //    x to its original place.
    return (x + (5-((x%5)||5))) * i;
}

Or to avoid logical operators, and just use arithmetic operators, we could do:

return (x + ((5-(x%5))%5)) * i;

And to spread it out a bit:

function toN5( x ) {
    var i = 1;
    while( x >= 100 ) {
        x/=10; 
        i*=10;
    }
    var remainder = x % 5;
    var distance_to_5 = (5 - remainder) % 5;
    return (x + distance_to_5) * i;
}
我的痛♀有谁懂 2024-12-11 12:15:51
var numbers = [51255, 25, 9214, 13135, 25123, 3, 6];

function weird_round(a) {
    var len = a.toString().length;
    var div = len == 1 ? 1 : Math.pow(10, len - 2);
    return Math.ceil(a / 5 / div) * div * 5;
}

alert(numbers.map(weird_round));

还针对 10 以下的数字进行了更新。对于负数也无法正常工作,如果需要,请提及。

演示

var numbers = [51255, 25, 9214, 13135, 25123, 3, 6];

function weird_round(a) {
    var len = a.toString().length;
    var div = len == 1 ? 1 : Math.pow(10, len - 2);
    return Math.ceil(a / 5 / div) * div * 5;
}

alert(numbers.map(weird_round));

Also updated for numbers below 10. Won't work properly for negative numbers either, just mention if you need this.

DEMO

眼趣 2024-12-11 12:15:51

我不知道为什么,但我认为正则表达式会很有趣:

    var result = +(number.toString().replace(/([1-9])([0-9])(.+)/, function() {
        return Math.ceil(+(arguments[1] + '.' + arguments[2])) * 10 - (+arguments[2] < 5?5:0) + arguments[3].replace(/./g, '0');
    }));

工作演示

I'm not sure why, but I thought it would be fun with regular expressions:

    var result = +(number.toString().replace(/([1-9])([0-9])(.+)/, function() {
        return Math.ceil(+(arguments[1] + '.' + arguments[2])) * 10 - (+arguments[2] < 5?5:0) + arguments[3].replace(/./g, '0');
    }));

Working Demo

你又不是我 2024-12-11 12:15:51
    with(Math) {
        var exp = floor(log(number)/log(10)) - 1;
        exp = max(exp,0);
        var n = number/pow(10,exp);
        var n2 = ceil(n/5) * 5;
        var result = n2 * pow(10,exp);
    }

http://jsfiddle.net/NvvGf/4/

注意:仅适用于自然数。

    with(Math) {
        var exp = floor(log(number)/log(10)) - 1;
        exp = max(exp,0);
        var n = number/pow(10,exp);
        var n2 = ceil(n/5) * 5;
        var result = n2 * pow(10,exp);
    }

http://jsfiddle.net/NvvGf/4/

Caveat: only works for the natural numbers.

自由如风 2024-12-11 12:15:51
function round(number) {
   var numberStr = number + "",
       max,
       i;

   if (numberStr[1] > '4') {
       numberStr[0] = parseInt(numberStr[0]) + 1;
       numberStr[1] = '0';
   } else {
       numberStr[1] = '5';
   }

   for (i = 2; max = numberStr.length; i < max; i += 1) {
      numberStr += '0';
   }

   return parseInt(numberStr);
}
function round(number) {
   var numberStr = number + "",
       max,
       i;

   if (numberStr[1] > '4') {
       numberStr[0] = parseInt(numberStr[0]) + 1;
       numberStr[1] = '0';
   } else {
       numberStr[1] = '5';
   }

   for (i = 2; max = numberStr.length; i < max; i += 1) {
      numberStr += '0';
   }

   return parseInt(numberStr);
}
羅雙樹 2024-12-11 12:15:51

奇怪的是,我不久前写过类似的东西!

function iSuckAtNames(n) {
    var n = n.toString(), len = n.length, res;

    //Check the second number. if it's less than a 5, round down,
    //If it's more/equal, round up

    //Either way, we'll need to use this:
    var res = parseFloat(n[0]) * Math.pow(10, len - 1); //e.g. 5 * 10^4 = 50000
    if (n[1] <= 5) {
        //we need to add a 5 right before the end!
        res += 5 * Math.pow(10, len - 2);
    }
    else {
        //We need another number of that size
        res += Math.pow(10, len - 1);
    }
    return res;
}

Strange coincidence, I wrote something really similar not so long ago!

function iSuckAtNames(n) {
    var n = n.toString(), len = n.length, res;

    //Check the second number. if it's less than a 5, round down,
    //If it's more/equal, round up

    //Either way, we'll need to use this:
    var res = parseFloat(n[0]) * Math.pow(10, len - 1); //e.g. 5 * 10^4 = 50000
    if (n[1] <= 5) {
        //we need to add a 5 right before the end!
        res += 5 * Math.pow(10, len - 2);
    }
    else {
        //We need another number of that size
        res += Math.pow(10, len - 1);
    }
    return res;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文