英文:
Find the value X where the number has been "rounded to the nearest X"
问题
什么是估算最近舍入的最简单方法,其中我们知道舍入始终是例如100、10、1、0.1等。我们不一定知道原始数字。
8 => 1 // 我们已经舍入到最近的 *一*
88 => 1
80 => 10 // 我们已经舍入到最近的 *十*
4530 => 10
4000 => 1000 // 我们已经舍入到最近的 *千*
0.024 => 0.001
0.02 => 0.01
0.024332 => 0.000001
4000.7 => 0.1 // 我们已经舍入到最近的 *十分*
这可能在给定一个数字时很有用,我们想要以相同的量舍入所有其他数字。感谢。
我正在尝试确定对于任何数字,如果我们假设该数字已被舍入,我们如何估算舍入量是多少?
例如,给定数字4000,我们估计我们开始时的数字是舍入到最近的千。但是给定4000.7,我们估计舍入是到最近的十分。
英文:
What is the simplest way to estimate the nearest rounding, where we know the rounding is always e.g. 100, 10, 1, 0.1 etc. We don't necessarily know the original number.
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*
This might be useful when, given one number, we want to round all other numbers by the same quantity. Thanks.
I am seeking to determine for any number, if we assume that the number has been rounded, how can we estimate what that rounding amount is?
For instance, given the number 4000, we estimate the number we started with was rounded to the nearest thousand. But given 4000.7, we estimate that the rounding was to the nearest tenth.
答案1
得分: 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(': ')
));
英文:
This is possibly a bit scrappy, but the idea is basically to find where the first thing that isn't zero is from the right and return a number based on that.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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(': ')
));
<!-- language: lang-html -->
<script crossorigin src="https://cdnjs.cloudflare.com/ajax/libs/decimal.js/9.0.0/decimal.min.js"></script>
<!-- end snippet -->
答案2
得分: 0
Raymond Chen已经正确理解了这一点 - 由于浮点表示的问题,您无法以数学方式完成这项任务,因此需要一个基于字符串的函数。
对于整数,我们可以使用正则表达式来计算尾随零的数量n,并且四舍五入将是10^n。
对于非整数,我们需要小数点后的数字数量n,同样,我们可以使用正则表达式来完成。四舍五入将是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);
}
}
以上是代码部分的翻译,您可以使用这个函数来进行四舍五入操作。
英文:
Raymond Chen has got this right - you cannot do this in a mathematical fashion, because of issues with floating point representations. So, you need a string-based function.
For integers we can use a RegEx to count the number of trailing zeros n, and the rounding will be 10^n.
For non-integers, we need the number of numbers n after the decimal point, again something we can do with RegEx. The rounding will be the inverse of 10^n.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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))
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论