找到数字已“四舍五入到最接近的 X”的值 X

问题描述 投票:0回答:3

估计最近舍入的最简单方法是什么,我们知道舍入总是例如100、10、1、0.1 等。我们不一定知道原始数字。

8        => 1         // we have rounded to the nearest *one*
88       => 1
80       => 10        // we have rounded to the nearest *ten* 
4530     => 10
4000     => 1000      // we have rounded to the nearest *thousand*
0.024    => 0.001
0.02     => 0.01
0.024332 => 0.000001
4000.7   => 0.1       // we have rounded to the nearest *tenth*

当给定一个数字,我们希望将所有其他数字四舍五入到相同数量时,这可能很有用。谢谢。

我正在寻求确定任何数字,如果我们假设该数字已四舍五入,我们如何估计该四舍五入的金额是多少?

例如,给定数字 4000,我们估计开始时的数字四舍五入到最接近的千位。但鉴于 4000.7,我们估计四舍五入到最接近的十分之一。

javascript math rounding precision
3个回答
0
投票

对于整数,这非常简单,只需从最高舍入到最低可能舍入,如果输入完全可整除(即模运算没有余数),那么这就是您的价值:

function getRounding(input){
   const values = [1000,100,10,1];
   
   for(var item of values){
     if((input % item) == 0){
        return item;
     }      
   }
}

console.log(getRounding(8))
console.log(getRounding(88))
console.log(getRounding(80))
console.log(getRounding(4530))
console.log(getRounding(4000))

不幸的是,由于浮点运算的性质,当您引入浮点值时,这并不能很好地工作。您需要重新考虑浮点数的问题。

function getRounding(input){
   const values = [1000,100,10,1, 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001, 0.0000001];
   
   for(var item of values){
     if((input % item) == 0){
        return item;
     }      
   }
}

console.log(getRounding(0.024))
console.log(getRounding(0.02))
console.log(getRounding(0.024332)) // Gone wrong here
console.log(getRounding(4000.7)) // Gone wrong here also


0
投票

这可能有点草率,但这个想法基本上是找到第一个不是零的东西从右边开始的地方,并根据它返回一个数字。

const get_last_non_zero_position = (num_str) => {
  let i = num_str.length - 1;
  
  for(; i >= 0; i--)
    if(num_str[i] !== '0')
      return i;
  
  return -1;
};

const get_rounding = (number) => {
  const [int_part, dec_part] = (number + '').split('.');
  
  if(dec_part?.length) {
    const dec_sig_digit = get_last_non_zero_position(dec_part);
    if(dec_sig_digit > -1)
      return '0.' + '0'.repeat(dec_sig_digit) + '1';
  }
  
  const int_sig_digit = get_last_non_zero_position(int_part);
  
  return '1' + '0'.repeat(int_part.length - int_sig_digit - 1);
};

const test_items = [
  8, 88, 80, 4530, 4000, 0.024, 0.02, 0.024332, 4000.7,
  0.1, 0.3, 0.2 + 0.1, new Decimal(0.2).plus(0.1)
];

console.log(test_items.map(
  (item) => [item, get_rounding(item)].join(': ')
));
<script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/decimal.js/9.0.0/decimal.min.js"></script>


0
投票

Raymond Chen 做对了——您 不能 以数学方式做到这一点,因为浮点表示存在问题。所以,你需要一个基于字符串的函数。

对于整数,我们可以使用正则表达式来计算尾随零的数量n,四舍五入为10^n。

对于非整数,我们需要小数点后的数字个数n,这也是我们可以用 RegEx 做的事情。四舍五入将是 10^n 的倒数。

function getRounding(n){
  
    const s = n.toString();
  
    if(Number.isInteger(n)){        
      return Math.pow(10, /0+$/.exec(s)?.[0].length ?? 0);      
    }
    else
    {     
      return 1/Math.pow(10, (/\.\d+/.exec(s)?.[0].length ?? 0) - 1);     
    }
  
}

console.log(getRounding(8))
console.log(getRounding(88))
console.log(getRounding(80))
console.log(getRounding(4530))
console.log(getRounding(4000))
console.log(getRounding(0.024))
console.log(getRounding(0.02))
console.log(getRounding(0.024332))
console.log(getRounding(4000.7))

© www.soinside.com 2019 - 2024. All rights reserved.