四舍五入到下一个整数 javascript
Round to the next whole number javascript
我想在 JavaScript 中实现这样的目标:
input = 2455.55
f(input) = 2456
f(input) = 2460
f(input) = 2500
f(input) = 3000
f(input) = 2455.55
我目前正在使用 Math.round()
方法,但只能达到 2,546。想知道是否有最好的方法来实现其余部分。
您可以将您的数字除以十,直到得到一个非整数,将其四舍五入,然后在相同的时间内再次乘以十。像这样:
function roundUp(n) {
var n2 = n;
var i=0;
while (Number.isInteger(n2)) {
n2 /= 10;
i++;
}
return Math.round(n2) * Math.pow(10, i);
}
console.log(roundUp(2455.55)); // 2456
console.log(roundUp(2456)); // 2460
console.log(roundUp(2460)); // 2500
console.log(roundUp(2500)); // 3000
谢谢,不错!受你的启发,我这样解决了:
function roundNumber(num, n) {
const divider = Math.pow(10, n);
return Math.round(num / divider) * divider;
};
根据您想要的输出,您似乎需要跟踪函数调用的次数。这似乎不是您的函数的参数。
鉴于您只有一个参数的限制,实现看起来可能像
var lastNum = 0
var digitsToRound = 0
function roundUp(input) {
// Verify whether the function is called with the same argument as last call.
// Note that we cannot compare floating point numbers.
// See https://dev.to/alldanielscott/how-to-compare-numbers-correctly-in-javascript-1l4i
if (Math.abs(input - lastNum) < Number.EPSILON) {
// If the number of digitsToRound exceeds the number of digits in the input we want
// to reset the number of digitsToRound. Otherwise we increase the digitsToRound.
if (digitsToRound > (Math.log10(input) - 1)) {
digitsToRound = 0;
} else {
digitsToRound = digitsToRound + 1;
}
} else {
// The function was called with a new input, we reset the digitsToRound
digitsToRound = 0;
lastNum = input;
}
// Compute the factor by which we need to divide and multiply to round the input
// as desired.
var factor = Math.max(1, Math.pow(10, digitsToRound));
return Math.ceil(input / factor) * factor;
}
console.log(roundUp(2455.55)); // 2456
console.log(roundUp(2455.55)); // 2460
console.log(roundUp(2455.55)); // 2500
console.log(roundUp(2455.55)); // 3000
我想在 JavaScript 中实现这样的目标:
input = 2455.55
f(input) = 2456
f(input) = 2460
f(input) = 2500
f(input) = 3000
f(input) = 2455.55
我目前正在使用 Math.round()
方法,但只能达到 2,546。想知道是否有最好的方法来实现其余部分。
您可以将您的数字除以十,直到得到一个非整数,将其四舍五入,然后在相同的时间内再次乘以十。像这样:
function roundUp(n) {
var n2 = n;
var i=0;
while (Number.isInteger(n2)) {
n2 /= 10;
i++;
}
return Math.round(n2) * Math.pow(10, i);
}
console.log(roundUp(2455.55)); // 2456
console.log(roundUp(2456)); // 2460
console.log(roundUp(2460)); // 2500
console.log(roundUp(2500)); // 3000
谢谢,不错!受你的启发,我这样解决了:
function roundNumber(num, n) {
const divider = Math.pow(10, n);
return Math.round(num / divider) * divider;
};
根据您想要的输出,您似乎需要跟踪函数调用的次数。这似乎不是您的函数的参数。
鉴于您只有一个参数的限制,实现看起来可能像
var lastNum = 0
var digitsToRound = 0
function roundUp(input) {
// Verify whether the function is called with the same argument as last call.
// Note that we cannot compare floating point numbers.
// See https://dev.to/alldanielscott/how-to-compare-numbers-correctly-in-javascript-1l4i
if (Math.abs(input - lastNum) < Number.EPSILON) {
// If the number of digitsToRound exceeds the number of digits in the input we want
// to reset the number of digitsToRound. Otherwise we increase the digitsToRound.
if (digitsToRound > (Math.log10(input) - 1)) {
digitsToRound = 0;
} else {
digitsToRound = digitsToRound + 1;
}
} else {
// The function was called with a new input, we reset the digitsToRound
digitsToRound = 0;
lastNum = input;
}
// Compute the factor by which we need to divide and multiply to round the input
// as desired.
var factor = Math.max(1, Math.pow(10, digitsToRound));
return Math.ceil(input / factor) * factor;
}
console.log(roundUp(2455.55)); // 2456
console.log(roundUp(2455.55)); // 2460
console.log(roundUp(2455.55)); // 2500
console.log(roundUp(2455.55)); // 3000