- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
谁能给我解释一下为什么会出现下面的代码:
public class Test {
public static void main(String... args) {
round(6.2088, 3);
round(6.2089, 3);
}
private static void round(Double num, int numDecimal) {
System.out.println("BigDecimal: " + new BigDecimal(num).toString());
// Use Locale.ENGLISH for '.' as decimal separator
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setGroupingUsed(false);
nf.setMaximumFractionDigits(numDecimal);
nf.setRoundingMode(RoundingMode.HALF_UP);
if(Math.abs(num) - Math.abs(num.intValue()) != 0){
nf.setMinimumFractionDigits(numDecimal);
}
System.out.println("Formatted: " + nf.format(num));
}
}
给出以下输出?
[me@localhost trunk]$ java Test
BigDecimal: 6.208800000000000096633812063373625278472900390625
Formatted: 6.209
BigDecimal: 6.208899999999999863575794734060764312744140625
Formatted: 6.208
以防您看不到:“6.2089”四舍五入为 3 位数给出输出“6.208”,而“6.2088”给出“6.209”作为输出。少即是多?
使用 Java 5、6 或 7 时结果很好,但是这个 Java 8 给了我这个奇怪的输出。Java 版本:
[me@localhost trunk]$ java -version
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) Server VM (build 25.5-b02, mixed mode)
编辑:这是 Java 7 的输出:
[me@localhost trunk]$ java Test
BigDecimal: 6.208800000000000096633812063373625278472900390625
Formatted: 6.209
BigDecimal: 6.208899999999999863575794734060764312744140625
Formatted: 6.209
Java 7 版本:
[me@localhost trunk]$ java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) Server VM (build 24.51-b03, mixed mode)
最佳答案
我可以将这个问题追溯到类 java.text.DigitList
的第 522 行。
情况是它认为小数位 6.0289
已经四舍五入(与等效的 BigDecimal
表示 6.208899...
相比,这是正确的) 并决定不再围捕。问题是这个决定只有在舍入后的数字是 5
的情况下才有意义,而不是当它大于 5
时。请注意 HALF_DOWN
的代码如何正确区分 digit=='5'
和 digit>'5'
的情况。
这显然是一个错误,而且是一个奇怪的错误,因为执行类似操作的代码(仅针对另一个方向)就在错误代码的正下方。
case HALF_UP:
if (digits[maximumDigits] >= '5') {
// We should not round up if the rounding digits position is
// exactly the last index and if digits were already rounded.
if ((maximumDigits == (count - 1)) &&
(alreadyRounded))
return false;
// Value was exactly at or was above tie. We must round up.
return true;
}
break;
case HALF_DOWN:
if (digits[maximumDigits] > '5') {
return true;
} else if (digits[maximumDigits] == '5' ) {
if (maximumDigits == (count - 1)) {
// The rounding position is exactly the last index.
if (allDecimalDigits || alreadyRounded)
/* FloatingDecimal rounded up (value was below tie),
* or provided the exact list of digits (value was
* an exact tie). We should not round up, following
* the HALF_DOWN rounding rule.
*/
return false;
else
// Value was above the tie, we must round up.
return true;
}
// We must round up if it gives a non null digit after '5'.
for (int i=maximumDigits+1; i<count; ++i) {
if (digits[i] != '0') {
return true;
}
}
}
break;
另一个数字没有发生这种情况的原因是 6.2088
不是四舍五入的结果(再次比较 BigDecimal
输出 6.208800…
)。所以在这种情况下它会四舍五入。
关于java - 仅 Java 8 的 NumberFormat 舍入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24426438/
我在 ASP.NET MVC 中有一个发票页面。 我正在计算 GST。 在 javascript 中这是结果。 165.45 * 0.1 = 16.544999999999998 在 C# 中我得到
在 JavaScript 中,我想将其减少到小数点后 5 位。不过,我不能简单地四舍五入并得到 0.03085,我需要进一步查看数字并将所有数字向上舍入,以便得到 0.03086。 输入:0.0308
有人可以解释为什么 R 这样做吗?在整数值上舍入最大值和最小值似乎非常有缺陷。 summary(1:1283932) Min. 1st Qu. Median Mean 3rd Qu.
所以基本上我正在做一个物理实验,在我的表格中,我希望我的数据四舍五入到与误差相同的精度,四舍五入为 1 sig fig。 例如,如果我有以下内容: angle signif(c(1.111,2.22
考虑以下 C# 代码... double x = Math.Round(72.6d, 2, MidpointRounding.ToZero); double y = Math.Round(82.6d,
我正在学习 BigDecimal,我希望它检索我输入的确切数字,以下代码正在处理该数字,我不知道为什么 public static BigDecimal parseFromNumberString(S
double y1 = 0; double y2 = 0; double i = 0.025; double n = 2; double h1 = 2000; double h2 = 4000
所以在下面的一组代码中,出于某种原因我得到了完全错误的答案...... import java.util.*; import java.io.*; import java.lang.*; import
在 Python 中,我想将两个数字相除,如果答案不是整数,我希望将数字四舍五入为上面的数字。 例如 100/30 不是给 33.3 而是给 4。谁能建议如何做到这一点?谢谢。 最佳答案 您可以使用
我需要对一个 float 进行四舍五入。例如 4.00011 。内置函数 round() 总是在数字 > .5 时向上舍入,在 = 0 val *= 10 ** precision r
我的代码: // Convert SATOSHIS to BITCOIN static double SATOSHI2BTC(const uint64_t& value) {
我想让我的 tableView 看起来像这样: 我有问题。只有在我点击单元格后,我的右角才会变圆。当 View 出现时,它看起来像这样: 点击后像这样: 这是我的代码: extension UITab
这个问题在这里已经有了答案: Precision String Format Specifier In Swift (31 个答案) 关闭 8 年前。 除了覆盖当前转换为字符串的方法之外,是否有一种
>>> a = 0.3135 >>> print("%.3f" % a) 0.314 >>> a = 0.3125 >>> print("%.3f" % a) 0.312 >>> 我期待 0.313
我有自动将输入字段加在一起的 javascript 函数,但是添加像 1.35 + 1.35 + 1.35 这样的数字会得到 4.050000000000001 的输出,这只是一个例子。如何将总数四舍
这可能是 x86 FPU 专家的问题: 我正在尝试编写一个生成范围 [min,max] 内的随机浮点值的函数。问题是我的生成器算法(浮点 Mersenne Twister,如果你好奇的话)只返回 [1
我一定错过了一些明显的东西。 select CEILING(85/30) = 2 85/30 = 2.83333 我希望该值为 3。 CEILING 函数不应该为我取整吗? 最佳答案 尝试 SELEC
我有一个关于 eclipse rcp 中的 ctabfolders 的问题。我创建了一个 e4 RCP 应用程序,其中包含一个包含堆栈部分容器的窗口,其中包含一个堆栈。该堆栈包含 1 个部分。在这一部
Closed. This question needs details or clarity。它当前不接受答案。
我读过其他一些帖子,它们似乎对其他人有用,但当我尝试它们时,它们不起作用。我刚刚开始学习Java编程,我似乎不明白如何四舍五入。 我试过了 answer = input * input; answer
我是一名优秀的程序员,十分优秀!