将大小数四舍五入到最接近的小数

时间:2021-05-11 00:38:53

标签: javascript rounding

所以我有一个四舍五入到特定小数位数的函数

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

我需要为此使用大数库吗?我想不出我会怎么做。

编辑:我已经用输入和输出值表更新了它,这样你就可以看到我想要实现的目标。我基本上希望数字根据它们与它们的接近程度来捕捉到某些值。与普通四舍五入类似,但略有不同。

      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

3 个答案:

答案 0 :(得分:1)

这不是很好,但我会把它放在这里,也许会回来完善它。不过,这应该会让你到达那里。 更新:由于我们没有使用正常的四舍五入,所以在函数的早期删除了 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));

答案 1 :(得分:1)

使用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"

答案 2 :(得分:0)

我认为您描述的算法是:

  1. 将数字除以比例
  2. 四舍五入得到一个整数
  3. 将比例乘以结果整数

换句话说:“如果我将这个数字分成大小为 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 });
});