javascript舍入函数

javascript舍入函数,javascript,math,rounding,Javascript,Math,Rounding,我想写一个函数,给定一个数字,它会将除第一个数字以外的所有数字设置为零,并将第一个数字增加一 例如,175应该变成200,23应该变成30,等等。最好的方法是什么?将数字除以,直到小于10(a.bcdefgh),记住你除以多少次,ceil,然后再次乘法 function superRoundUp(n) { i = 0; while (n > 10) { i++; n = n/10; } n = Math.ceil(n);

我想写一个函数,给定一个数字,它会将除第一个数字以外的所有数字设置为零,并将第一个数字增加一


例如,175应该变成200,23应该变成30,等等。最好的方法是什么?

将数字除以,直到小于10(a.bcdefgh),记住你除以多少次,ceil,然后再次乘法

function superRoundUp(n) {
    i = 0;
    while (n > 10) {
        i++;
        n = n/10; 
    }
    n = Math.ceil(n);
    for ( ; i>0; i--) {
        n = n * 10;
    }
    return n;
}
var n = 57;
alert(superRoundUp(n));

如果您想在十进制中进行操作,有时最好的方法就是将其视为十进制数字字符串

function oneSignificantDigitAwayFromZero(n) {
   // Convert to a string of digits.
   var s = "" + n;
   // This regexp grabs any sign, and leading zeros in one group,
   // the digit to promote in another, and the trailing digits in a third.
   // This regexp is guaranteed not to accidentally grab any exponent.
   return s.replace(/^(-?[0.]*)([1-9])([0-9.]+)/, function (_, before, digit, after) {
     // Round the digit up if there is a non-zero digit after it,
     // so 201 -> 300, but 200 -> 200.
     var roundUp = /[1-9]/.test(after) ? +digit + 1 : +digit;
     // Replace all non-zero digits after the one we promote with zero.
     // If s is "201", then after is "01" before this and "00" after.
     after = after.replace(/[1-9]/g, "0");
     // If roundUp has no carry, then the result is simple.
     if (roundUp < 10) { return before + roundUp + after; }
     // Otherwise, we might have to put a dot between the 1 and 0 or consume a zero from
     // the fraction part to avoid accidentally dividing by 10. 
     return before.replace(/0?([.])?$/, "1$10") + after;
   });
}
函数onesignifictdigitalwayfromzero(n){
//转换为一串数字。
var s=”“+n;
//此regexp获取任何符号,并在一个组中前导零,
//要升级的数字在另一个中,后面的数字在第三个中。
//此regexp保证不会意外获取任何指数。
返回s.replace(/^(-[0.]*)([1-9])([0-9.]+)/,函数(_,before,digital,after){
//如果后面有非零数字,则将数字向上舍入,
//所以201->300,但是200->200。
变量汇总=/[1-9]/。测试(在?+位+1+位之后);
//将升级后的所有非零数字替换为零。
//如果s为“201”,则之后在此之前为“01”,之后为“00”。
after=after.替换(/[1-9]/g,“0”);
//如果roundUp没有进位,那么结果很简单。
if(roundUp<10){return before+roundUp+after;}
//否则,我们可能必须在1和0之间放置一个点,或者从中消耗一个0
//分数部分,以避免意外除以10。
返回之前。替换(/0?([.])?$/,“1$10”)+之后;
});
}
那很有趣:D

对于未命名的“其他人”:

简短版本:

function myRound(num)
{
    var pow = Math.pow(10, String(num).length - 1);
    return Math.ceil(num/pow)*pow;
}
测试:

> myRound(175)
  200
> myRound(23)
  30
> myRound(95)
  100

好的,再来一个,使用一些字符串魔术。与Joseph的答案类似,但您避免使用任何浮点运算(仍然不确定哪一种可能更有效):


这里的大多数Answear都使用字符串。那怎么处理负数呢?浮点数?我的解决方案只使用数学函数,并且适用于所有数字(我认为)

有关函数和一些测试用例,请参见链接:)


干杯

函数A(A){var b=Math.abs(A);返回((b+“”)[0]/1+1)*Math.pow(10,(b+“”).length-1)*(a应该变成100,用0和9+1=10替换5,得到100,那么像-25这样的负数呢?对你们所有人来说,你们都在使用字符串。这对负数和浮点数都不起作用。你们可以通过四舍五入和取绝对值来解决这个问题。这就像我的一样,不管怎样,做得很好,我们都认为是一样的XD。有什么评论吗w您可以使用n.toString();负数前面会有一个减号,因此长度将是一到长。FloatNumber也会有不同的长度。您可以在转换为字符串+1
(num+“”)时通过取num的abs值来处理负数。length
稍微快一点(至少在我测试它的FF5中)我喜欢你的回答,虽然很简洁。谢谢你的建议。我想这次我涵盖了所有内容:P
function myRound(num)
{
    var digits = String(num).length - 1,
        pow = Math.pow(10, digits);
    num /= pow;
    num = Math.ceil(num);
    num *= pow;
    return num;
}
function myRound(num)
{
    var pow = Math.pow(10, String(num).length - 1);
    return Math.ceil(num/pow)*pow;
}
> myRound(175)
  200
> myRound(23)
  30
> myRound(95)
  100
function roundUp(number)
{
    var numberStr = number.toString();
    var firstDigit = parseInt(numberStr.substring(0, 1)) + 1;
    return firstDigit * Math.pow(10, (numberStr.length - 1));
};
alert(roundUp(23));
function A(a){var b=Math.abs(a);return((b+'')[0]/1+1)*Math.pow(10,(b+'').length-1)*(a<0?-1:1)}