javascript rounding function
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);
}
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
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));
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).
http://jsfiddle.net/xBVjB/7/
See link for function and some testcases :)
Cheers
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.
精彩评论