[读书笔记][Effective Java]不要在精确计算中使用float和double类型
来源:互联网 发布:利用淘宝客漏洞赚钱 编辑:程序博客网 时间:2024/03/28 19:44
在精确计算,尤其是有关金钱的商业运算中,不能使用float和double类型。
看如下的例子:
商店里某种糖果的价格是0.1元,0.2元,0.3元, …… 依此类推,一直到1.00元。现在你手中有1元钱。你想买一些糖果,假设你从1角的糖果开始依次买,一种价格的买一颗。计算一下一共可以买多少颗糖果,最后会剩下多少零钱。
第一个程序:
package com.mytest;
public class Test {
private static final double FUNDS = 1.00;
private static final double TEN_CENTS = .1;
public static void main( String[] args ) {
int itemsBought = 0;
double myfund = FUNDS;
for (double price = TEN_CENTS; price < myfund; price+= TEN_CENTS) {
itemsBought++;
myfund-= price;
}
System.out.println("itemsBought: " + itemsBought);
System.out.println("myfund left: " + myfund);
}
}
在这里,使用double类型,进行计算,但是却得到如下结果:
itemsBought: 3
myfund left: 0.3999999999999999
这并不是正确的结果。在涉及金钱的运算中,应该使用BigDecimal。
正确的程序如下:
package com.mytest;
import java.math.BigDecimal;
public class Test {
private static final BigDecimal FUNDS = new BigDecimal("1.00");
private static final BigDecimal TEN_CENTS = new BigDecimal(".10");
public static void main( String[] args ) {
int itemsBought = 0;
BigDecimal myfund = FUNDS;
for (BigDecimal price = TEN_CENTS; price.compareTo(myfund) <= 0; price = price.add(TEN_CENTS)) {
itemsBought++;
myfund = myfund.subtract(price);
}
System.out.println("itemsBought: " + itemsBought);
System.out.println("change: " + myfund);
}
}
打印结果:
itemsBought: 4
change: 0.00
这才是正确的答案。
In summary, don't use float or double for any calculations that require an exact answer. User BigDecimal if you want the system to keep track of the decimal point and you don't mind the inconveneice of not using a primitive type. Using BigDecimal has the added advantage that it gives you full controll over rounding, letting you select from eight rounding modes whenever an operation that entails rounding is performed. This comes in handy if you're performing business calculations with legally mandated rounding behaviour. If performance is of the essence, if you don't mind keeping track of the decimal point yourself, and if the quantities aren't too big, use int or long. If the quantitiest don't exceed nine decimal digits, you can use int; if they don't exceed eighteen digits, you can use long. If the quantities exceed eighting digits, you must use BigDecimal.
- [读书笔记][Effective Java]不要在精确计算中使用float和double类型
- 不要在精确计算中使用float和double类型
- Effective Java读书笔记--如果想要知道精确的答案,就要避免使用double和float
- 在对于精确计算的要求下请不要使用float和double
- 精确计算java中float和double的精度
- 天天记录 - Java 精确计算避免使用float和double
- 精确计算时,不要使用float或double
- JAVA中实现double类型精确计算
- 做精确计算时应避免使用float和double
- Java中浮点型数据Float和Double进行精确计算的问题
- Java 精确计算-double-float-String
- Java:Effective Java 学习笔记(第48条:如果需要精确的答案,请避免使用float和double)
- 不要用float和double来进行精确的小数计算
- 精确运算避免使用float和double
- Java中double float 类型的数据(小数)在作计算的时候要注意的
- java中double、float类型计算精度丢失问题
- Java中用浮点型数据Float和Double进行精确计算时的精度问题
- Java中用浮点型数据Float和Double进行精确计算时的精度问题
- Dun & Bradstreet公司高度评价SOA治理
- Introducing Windows Mobile 5.0
- 连接池的使用
- 广州2006车展现场。。。美女好车如云哦!
- Q & F
- [读书笔记][Effective Java]不要在精确计算中使用float和double类型
- .NET读取Excel数据为null的解决办法
- [资料库]DZ程序文件目录含义整理表
- 迷失的季节联播
- 收藏的一些网址
- 大家都来看迷失的季节吧,瓦哈哈
- 大家都来看迷失的季节吧,瓦哈哈
- 大家都来看迷失的季节吧,瓦哈哈
- 大家都来看迷失的季节吧,瓦哈哈