繁体   English   中英

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

[英]Rounding large decimals to nearest decimals

所以我有一个 function 可以四舍五入到某些十进制

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

我希望能够从这个数字中获得 output 任何这些金额

 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

这不漂亮,但我会把它放在这里,也许会回来完善它。 不过,这应该可以帮助您。 更新:在 function 早期删除了 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"

我认为您描述的算法是:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM