将大的小数四舍五入到最接近的小数
Rounding large decimals to nearest decimals
所以我有一个函数可以四舍五入到特定的小数位
const roundNumber = (num, scale) => {
let n = num
switch(scale){
case 0.50:
n = (Math.ceil(num*2)/2).toFixed(2)
break;
case 0.25:
n = (Math.ceil(num*4)/4).toFixed(2)
break;
case 0.20:
n = (Math.ceil(num*5)/5).toFixed(2)
break;
case 0.10:
n = (Math.ceil(num*10)/10).toFixed(2)
break;
case 0.05:
n = (Math.ceil(num*20)/20).toFixed(2)
break;
}
return n
}
我如何将其调整为更小的数额,例如...
0.000001, 0.000025, 0.000050, 0.0001,0.00025, 0.00050, 0.001 etc etc
举个例子。
0.48675387
我希望能够从这个数字中输出这些金额中的任何一个
0.48675000
0.48680000
0.48700000
0.49000000
0.50000000
为此我需要使用大数库吗?我想不出我该怎么做。
编辑:我用 table 的输入和输出值更新了它,这样你就可以看到我想要实现的目标。我基本上希望数字根据它们与它们的接近程度捕捉到某些值。与正常舍入类似,但略有不同。
Scale Number Output
0.00025 0.45341000 0.45350
0.0025 0.45341000 0.4525 (closer to 0.4525 than 0.4550
0.005 0.45341000 0.455
0.01 0.45341000 0.45
如果我只是使用 toPrecision 或 toFixed,看看值有何不同
Number Output
0.45341000 0.45341
0.45341000 0.4534
0.45341000 0.453
0.45341000 0.45
这不是很好,但我会把它放在这里,也许回来完善它。不过,这应该可以让您到达那里。 更新:在函数的早期删除了 toPrecision,因为我们没有使用正常舍入。
function roundTo(num, scale) {
let arr, places, ref, lastx, remainder, thenum
arr = scale.toString().split(".")
places = arr[1].length; // get number decimal places
num = Number(num).toFixed(places); // get our starting number in the right format
ref=scale * Math.pow(10, places); // get our scale to the right format
lastx = Number(num.toString().substr(-(ref.toString().length))) // get the scale increment
remainder = lastx%ref; // modulus
if (remainder>=ref/2) thenum= Number((ref-remainder)*Math.pow(.1, places)) // if we need to add an amt
else thenum = Number(remainder*Math.pow(.1, places)) * -1 ; // if we need to substract an amt
thenum = thenum.toFixed(places) // fix our value
// console.log(places,num,ref,lastx,remainder,thenum);
num=(Number(num) + Number(thenum)).toFixed(places) // final calculation
return num;
}
console.log(roundTo(0.8946, 0.001));
console.log(roundTo(1.8946, 0.001));
使用Number.prototype.toPrecision()
let n = 0.48675387
n.toPrecision(5)
"0.48675"
n.toPrecision(4)
"0.4868"
n.toPrecision(3)
"0.487"
n.toPrecision(2)
"0.49"
n.toPrecision(1)
"0.5"
我认为您描述的算法是:
- 将数字除以比例
- 四舍五入得到一个整数
- 将比例乘以结果整数
换句话说:“如果我将这个数字分成大小为 scale
的块,如果余数超过 scale
数量的一半,则再添加一个块,然后将块重新组合在一起,总和是多少?
function roundTo(scale, number) {
return Math.round(number / scale) * scale;
}
function roundTo(scale, number) {
return Math.round(number / scale) * scale;
}
const examples = [
{ scale: 0.00025, number: 0.45341000, expected: 0.45350 },
{ scale: 0.0025, number: 0.45341000, expected: 0.4525 },
{ scale: 0.005, number: 0.45341000, expected: 0.455 },
{ scale: 0.01, number: 0.45341000, expected: 0.45 },
]
examples.forEach(({ scale, number, expected }) => {
const actual = roundTo(scale, number);
const isExpectedResult = actual === expected ? '✅' : '❌';
console.log({ scale, number, expected, actual, isExpectedResult });
});
所以我有一个函数可以四舍五入到特定的小数位
const roundNumber = (num, scale) => {
let n = num
switch(scale){
case 0.50:
n = (Math.ceil(num*2)/2).toFixed(2)
break;
case 0.25:
n = (Math.ceil(num*4)/4).toFixed(2)
break;
case 0.20:
n = (Math.ceil(num*5)/5).toFixed(2)
break;
case 0.10:
n = (Math.ceil(num*10)/10).toFixed(2)
break;
case 0.05:
n = (Math.ceil(num*20)/20).toFixed(2)
break;
}
return n
}
我如何将其调整为更小的数额,例如...
0.000001, 0.000025, 0.000050, 0.0001,0.00025, 0.00050, 0.001 etc etc
举个例子。
0.48675387
我希望能够从这个数字中输出这些金额中的任何一个
0.48675000
0.48680000
0.48700000
0.49000000
0.50000000
为此我需要使用大数库吗?我想不出我该怎么做。
编辑:我用 table 的输入和输出值更新了它,这样你就可以看到我想要实现的目标。我基本上希望数字根据它们与它们的接近程度捕捉到某些值。与正常舍入类似,但略有不同。
Scale Number Output
0.00025 0.45341000 0.45350
0.0025 0.45341000 0.4525 (closer to 0.4525 than 0.4550
0.005 0.45341000 0.455
0.01 0.45341000 0.45
如果我只是使用 toPrecision 或 toFixed,看看值有何不同
Number Output
0.45341000 0.45341
0.45341000 0.4534
0.45341000 0.453
0.45341000 0.45
这不是很好,但我会把它放在这里,也许回来完善它。不过,这应该可以让您到达那里。 更新:在函数的早期删除了 toPrecision,因为我们没有使用正常舍入。
function roundTo(num, scale) {
let arr, places, ref, lastx, remainder, thenum
arr = scale.toString().split(".")
places = arr[1].length; // get number decimal places
num = Number(num).toFixed(places); // get our starting number in the right format
ref=scale * Math.pow(10, places); // get our scale to the right format
lastx = Number(num.toString().substr(-(ref.toString().length))) // get the scale increment
remainder = lastx%ref; // modulus
if (remainder>=ref/2) thenum= Number((ref-remainder)*Math.pow(.1, places)) // if we need to add an amt
else thenum = Number(remainder*Math.pow(.1, places)) * -1 ; // if we need to substract an amt
thenum = thenum.toFixed(places) // fix our value
// console.log(places,num,ref,lastx,remainder,thenum);
num=(Number(num) + Number(thenum)).toFixed(places) // final calculation
return num;
}
console.log(roundTo(0.8946, 0.001));
console.log(roundTo(1.8946, 0.001));
使用Number.prototype.toPrecision()
let n = 0.48675387
n.toPrecision(5)
"0.48675"
n.toPrecision(4)
"0.4868"
n.toPrecision(3)
"0.487"
n.toPrecision(2)
"0.49"
n.toPrecision(1)
"0.5"
我认为您描述的算法是:
- 将数字除以比例
- 四舍五入得到一个整数
- 将比例乘以结果整数
换句话说:“如果我将这个数字分成大小为 scale
的块,如果余数超过 scale
数量的一半,则再添加一个块,然后将块重新组合在一起,总和是多少?
function roundTo(scale, number) {
return Math.round(number / scale) * scale;
}
function roundTo(scale, number) {
return Math.round(number / scale) * scale;
}
const examples = [
{ scale: 0.00025, number: 0.45341000, expected: 0.45350 },
{ scale: 0.0025, number: 0.45341000, expected: 0.4525 },
{ scale: 0.005, number: 0.45341000, expected: 0.455 },
{ scale: 0.01, number: 0.45341000, expected: 0.45 },
]
examples.forEach(({ scale, number, expected }) => {
const actual = roundTo(scale, number);
const isExpectedResult = actual === expected ? '✅' : '❌';
console.log({ scale, number, expected, actual, isExpectedResult });
});