Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将大小数舍入为最接近的小数_Javascript_Rounding - Fatal编程技术网

Javascript 将大小数舍入为最接近的小数

Javascript 将大小数舍入为最接近的小数,javascript,rounding,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

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

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

这并不漂亮,但我会把它放在这里,也许会回来完善它。这应该能让你达到目的。更新:在函数的早期删除了toPrecision,因为我们不使用正常舍入


这并不漂亮,但我会把它放在这里,也许会回来完善它。这应该能让你达到目的。更新:在函数的早期删除了toPrecision,因为我们不使用正常舍入


使用
Number.prototype.toPrecision()


使用
Number.prototype.toPrecision()


我认为你描述的算法是:

  • 将数字除以刻度
  • 将该结果四舍五入得到一个整数
  • 将比例乘以结果整数
  • 换句话说:“如果我把这个数字分成大小为
    scale
    的块,如果剩余的块超过
    scale
    数量的一半,那么再加一块,然后把这些块放在一起,总数是多少?”

    函数取整(比例、数字){
    返回数学四舍五入(数字/刻度)*刻度;
    }
    
    函数取整(比例、数字){
    返回数学四舍五入(数字/刻度)*刻度;
    }
    常量示例=[
    {比例:0.00025,数字:0.45341000,预期:0.45350},
    {比例:0.0025,数字:0.45341000,预期值:0.4525},
    {比例:0.005,数字:0.45341000,预期:0.455},
    {比例:0.01,数字:0.45341000,预期值:0.45},
    ]
    示例.forEach({scale,number,expected})=>{
    实际常数=四舍五入(比例、数量);
    常量isExpectedResult=实际===预期值?'✅' : '❌';
    log({scale,number,expected,actual,isExpectedResult});
    
    });我认为您描述的算法是:

  • 将数字除以刻度
  • 将该结果四舍五入得到一个整数
  • 将比例乘以结果整数
  • 换句话说:“如果我把这个数字分成大小为
    scale
    的块,如果剩余的块超过
    scale
    数量的一半,那么再加一块,然后把这些块放在一起,总数是多少?”

    函数取整(比例、数字){
    返回数学四舍五入(数字/刻度)*刻度;
    }
    
    函数取整(比例、数字){
    返回数学四舍五入(数字/刻度)*刻度;
    }
    常量示例=[
    {比例:0.00025,数字:0.45341000,预期:0.45350},
    {比例:0.0025,数字:0.45341000,预期值:0.4525},
    {比例:0.005,数字:0.45341000,预期:0.455},
    {比例:0.01,数字:0.45341000,预期值:0.45},
    ]
    示例.forEach({scale,number,expected})=>{
    实际常数=四舍五入(比例、数量);
    常量isExpectedResult=实际===预期值?'✅' : '❌';
    log({scale,number,expected,actual,isExpectedResult});
    

    }); 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));
    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"