3

I want to write a function that given a number will set all but the first digit to zero and will increase the first digit by one

for example, 175 should become 200, 23 should become 30, etc. What is the best way to do this?

3
  • should become 100, replace 5 with a 0 and 9+1=10 and get 100 Commented Aug 2, 2011 at 18:00
  • 1
    What about negative numbers like -25? Commented Aug 2, 2011 at 18:41
  • To all of you you are using strings. that will not work for negative number nor floating number. You can work around that with rounding and taking the absolute though. Commented Aug 2, 2011 at 18:45

7 Answers 7

3
function truncUp(num) {
    var factor = Math.pow(10, (num+'').toString().length - 1);
    return Math.floor(num / factor) * factor + factor;
}

that was fun :D

And for the unnamed "others":

function truncUp(num) {
    num = Math.floor(num);
    var factor = Math.pow(10, (Math.abs(num)+'').toString().length - 1);
    return Math.floor(num / factor) * factor + ((Math.abs(num) - num == 0?1:2)*factor);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Negative numbers will have a minus sign in front of it, and length will therefore be one to long. Floatnumbers will also have a different length. You can handle negative number by taking the abs-value of num when converting to string
+1 (num+'').length is marginally faster (at least in FF5 where I tested it) and shorter as well. Liked your answer though very concise.
Thanks for the advice qw3n and Rickard :) I think I covered everything this time :P
1
function myRound(num)
{
    var digits = String(num).length - 1,
        pow = Math.pow(10, digits);
    num /= pow;
    num = Math.ceil(num);
    num *= pow;
    return num;
}

Short version:

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

Tests:

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

1 Comment

that's just like mine O_o nicely done anyhow XD we think alike.
0

Divide the number until it is less then 10 (a.bcdefgh), remember how many times you divided, ceil, then multiply again.

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));

Comments

0

Ok, one more, using some String magic. Similar to Josephs answer, but you avoid using any floating point operations (still not sure which one might be more efficient):

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));

Comments

0

If you want to manipulate in decimal, sometimes the best way is to just treat it as a string of decimal digits.

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;
   });
}

1 Comment

comments much? anyhow you can use n.toString();
0

Most of the answears here uses strings. How will that handle negativ number? Float numbers? My solution uses only Mathematical functions and and works for all numbers (i think).

http://jsfiddle.net/xBVjB/7/

See link for function and some testcases :)

Cheers

Comments

0
function A(a){var b=Math.abs(a);return((b+'')[0]/1+1)*Math.pow(10,(b+'').length-1)*(a<0?-1:1)}

Here is my answer manipulating a string. It handles negatives, but I am not sure how the OP wants negatives rounded up/down.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.