java中的十进制数总和 [英] Sum of decimal number in java

查看:177
本文介绍了java中的十进制数总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java(JDK 1.4)中十进制数管理的问题。



我有两个双重数字第一个(作为格式化的 String 的输出)。我在第一和第二之间做一个总和,我收到一个数字更多的十进制数字!

  final double first = 198.4; //通过不可修改的格式方法的值提取

final double second = 44701.2; //通过不可修改的格式方法获取的值

final double firstDifference = first + second; //我收到44899.598而不是44899.6

final double calculatedDifference = 44900.1; //流的比较值

final double error = firstDifference-calculatedDifference; //我收到-0.50390605代替0.5

if(Math.abs(error)= 0.5d)){
//我必须输入这个分支,但错误不允许正确的流程!
}
/ ***
*程序流程不正确,业务视图中有一个非常明显的错误
* /

我不喜欢增加阈值( 0.5d ),因为我没有类似情况的安全(当我开始编码,这个规范正在谈论 0.1d 作为比较值)。
如果这是唯一的解决方案, 0.9d 的值对于这个问题是最安全的?



我如何解决这个问题?我认为这个问题是通过使用双变量得出的,但是与float我有同样的问题。



有些想法(有可能的测试代码行))

解决方案

你可以得到四舍五入的错误,但我在这里看不到。

  final double first = 198.4; //通过不可修改的格式方法获取值
final double second = 44701.2; //通过不可修改的格式方法获得的值
final double firstDifference = first + second; //我收到44899.6
final double calculatedDifference = 44900.1; //流量的比较值
final double error = firstDifference-calculatedDifference; //我收到-0.5

if(Math.abs(error)< = 0.5d){
//输入此分支。
System.out.println(error);
}

打印

  -0.5 

有两种方法可以更一般的处理。您可以定义一个舍入误差,如

  private static final double ERROR = 1e-9; 

if(Math.abs(error)< = 0.5d + ERROR){

或使用舍入

  final double firstDifference = round(first + second,1); //调用一个函数来舍入到一个小数位。 

或使用固定精度的整数

  final int first = 1984; // 198.4 * 10 
final int second = 447012; // 44701.2 * 10
final int firstDifference = first + second; //我收到448996
final int calculatedDifference = 449001; //流的比较值
final int error = firstDifference-calculatedDifference; //我收到-5

if(Math.abs(error)= 5){
//输入这个分支。
System.out.println(error);
}

或者您可以使用BigDecimal。这通常是许多开发人员的首选解决方案,但最后一个选项是IMHO。 ;)


I have a problem for the managemente of decimal number in java (JDK 1.4).

I have two double numbers first and second (as output of formatted String). I do a sum between fist and second and I receive a number with more decimal digits!

   final double first=198.4;//value extract by unmodifiable format method

   final double second=44701.2;//value extract by unmodifiable format method

   final double firstDifference= first+second; //I receive 44899.598 instead of 44899.6

   final double calculatedDifference=44900.1; // comparison value for the flow

    final double error=firstDifference-calculatedDifference;// I receive -0.50390605 instead 0.5

    if(Math.abs(error)<=0.5d)){
         //I must enter in this branch but the error not allows the correct flow!!!
    }
    /***
    * the flow of program is uncorrect and there's a very visible bug in business view
    */

I prefer not growing the threshold value (0.5d) because I'm not safe with similar situation (when I started coding, the specs was talking about 0.1d as comparison value). If it's the only solution, the value of 0.9d is safest value for this problem?

How I can resolve this situation? I thinked that this problem derive by the use of double variables, but with the float I have the same problem.

Some idea (having some tested code line, if possible ;))?

解决方案

You can get rounding error, but I don't see it here.

final double first=198.4;//value extract by unmodifiable format method
final double second=44701.2;//value extract by unmodifiable format method
final double firstDifference= first+second; //I receive 44899.6
final double calculatedDifference=44900.1; // comparison value for the flow
final double error=firstDifference-calculatedDifference;// I receive -0.5 

if(Math.abs(error)<=0.5d){
    // this branch is entered.
    System.out.println(error);
}

prints

-0.5

There are two ways to handle this more generally. You can define a rounding error such as

 private static final double ERROR = 1e-9;

 if(Math.abs(error)<=0.5d + ERROR){

OR use rounding

final double firstDifference= round(first+second, 1); // call a function to round to one decimal place.

OR use integers with fixed precision

final int first=1984;// 198.4 * 10
final int second=447012; // 44701.2 * 10
final int firstDifference= first+second;  //I receive 448996
final int calculatedDifference=449001; // comparison value for the flow
final int error=firstDifference-calculatedDifference;// I receive -5 

if(Math.abs(error)<=5){
    // this branch is entered.
    System.out.println(error);
}

OR You can use BigDecimal. This is often the preferred solution for many developers, but a last option IMHO. ;)

这篇关于java中的十进制数总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆