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?
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?
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);
}
(num+'').length is marginally faster (at least in FF5 where I tested it) and shorter as well. Liked your answer though very concise.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
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));
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;
});
}
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).
See link for function and some testcases :)
Cheers