Effective Java 48 Avoid float and double if exact answers are required
Reason
The float and double types are particularly ill-suited for monetary calculations because it is impossible to represent 0.1 (or any other negative power of ten) as a float or double exactly.
// This will print 0.6100000000000001
System.out.println(1.03 - .42);
// This will print 0.09999999999999998
System.out.println(1.00 - 9 * .10);
Right way
Use BigDecimal , int , or long for monetary calculations
public static void main(String[] args) {
final BigDecimal TEN_CENTS = new BigDecimal( ".10");
int itemsBought = 0;
BigDecimal funds = new BigDecimal("1.00");
for (BigDecimal price = TEN_CENTS; funds.compareTo(price) >= 0; price = price.add(TEN_CENTS)) {
itemsBought++;
funds = funds.subtract(price);
}
System.out.println(itemsBought + " items bought.");
System.out.println("Money left over: $" + funds);
}
|
Advantage |
|
|
Disadvantage |
|
An alternative to using BigDecimal
Using int or long, depending on the amounts involved, and to keep track of the decimal point yourself.
public static void main(String[] args) {
int itemsBought = 0;
int funds = 100;
for (int price = 10; funds >= price; price += 10) {
itemsBought++;
funds -= price;
}
System.out.println(itemsBought + " items bought.");
System.out.println("Money left over: "+ funds + " cents");
}
Decision on choosing implementation
|
Quantities |
9 |
18 |
>18 |
|
Use |
int |
long |
BigDecimal |
Summary
Don't use float or double for any calculations that require an exact answer. Use BigDecimal if you want the system to keep track of the decimal point and you don't mind the inconvenience and cost of not using a primitive type.
Effective Java 48 Avoid float and double if exact answers are required的更多相关文章
- java中int,float,long,double取值范围,内存泄露
java中int,float,long,double取值范围是多少? 写道 public class TestOutOfBound { public static void main(String[] ...
- Java中的float、double计算精度问题
java中的float.double计算存在精度问题,这不仅仅在java会出现,在其他语言中也会存在,其原因是出在IEEE 754标准上. 而java对此提供了一个用于浮点型计算的类——BigDeci ...
- 【转】JAVA程序中Float和Double精度丢失问题
原文网址:http://blog.sina.com.cn/s/blog_827d041701017ctm.html 问题提出:12.0f-11.9f=0.10000038,"减不尽" ...
- Effective Java 50 Avoid strings where other types are more appropriate
Principle Strings are poor substitutes for other value types. Such as int, float or BigInteger. Stri ...
- Effective Java 67 Avoid excessive synchronization
Principle To avoid liveness and safety failures, never cede control to the client within a synchroni ...
- java 小心使用float和double他可能不如你所想
public static void main(String[] args) { double funds=1.00; ; // ; ;funds>=price;price+=.){ funds ...
- Effective Java 07 Avoid finallizers
NOTE Never do anything time-critical in a finalizer. Never depend on a finalizer to update critical ...
- Effective Java 73 Avoid thread groups
Thread groups were originally envisioned as a mechanism for isolating applets for security purposes. ...
- Effective Java 05 Avoid creating unnecessary objects
String s = new String("stringette"); // Don't do this. This will create an object each tim ...
随机推荐
- JAVA魔法堂:折腾Mybatis操作SQLite的SQLException:NYI异常
一.坑爹 在使用Mybatis3.2.7+sqlite-jdbc3.3时,在执行查询操作,不管returnType和parameterType传什么值均报位于mapper.xml文件中的java.sq ...
- IOS开发UI基础文本属性Attributes
文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFo ...
- 如何转移数据库MDF和LDF文件
我们可以很轻易地使用SQL Server来创建一个数据库,创建的数据库实例将存储在指定的默认位置(不一定是C盘,可以手动变更默认存储位置).假设此时数据库实例创建在了C盘中的默认位置,亦即是与数据库安 ...
- Scrum 项目5.0--软件工程
1.燃尽图: 2.每日立会更新任务板上任务完成情况.燃尽图的实际线,分析项目进度是否在正轨. 每天的例会结束后的都为任务板拍照并发布到博客上 团队贡献分: 蔡舜 : 21 卢晓洵 : 1 ...
- 《构建之法》第8、9、10章的读后感和第一个sprint总结
第八章——主要介绍软件需求. 主要步骤:1.获取和引导需求.2.分析和定义需求.3.验证需求.4.在软件产品的生命周期中管理需求. 对软件需求的划分:1.对产品功能性的需求.2.对产品开发过程的需求. ...
- SQL Server存储过程复习(一)
--存储过程学习篇 --.简单存储过程不带参数的学习 IF OBJECT_ID('Orders_GetAllOrders','P') IS NOT NULL DROP PROCEDURE Orders ...
- Sql发布订阅设置不初始化订阅库架构的设置
参考:http://www.cnblogs.com/TeyGao/p/3521231.html
- n个人作m幅画
题目网址: http://codeforces.com/problemset/problem/416/B A well-known art union called "Kalevich is ...
- [原创]winform_PC宴会图片抽奖/文字抽奖
14年6月 好友结婚 14年4月左右知道他们婚礼由迎宾照抽奖的环节 问我有没有可以用的抽奖软件 我网上找了一会儿,就放弃了,自己做一个更快不是? 14年6月,PC宴会图片抽奖软件成功使用 --- 操作 ...
- css三角形的实现
实底三角形: <html> <head> <title></title> <style type="text/css"> ...