java 小心使用float和double他可能不如你所想
public static void main(String[] args) {
double funds=1.00;
int itemBought=;
//
double price=.;
for(price=.;funds>=price;price+=.){
funds-=price;
itemBought++;
}
//#解释1
// 第一次 price=0.1 funds=1.00
// #1 #2 #3
// for(double price=.1;funds>=price;price+=.10)
//不经过#2,#3 price仍然为0.1 进入for循环 执行 funds-=prcie 此时funds=1 ,price=0.1 ,结果funds=0,9
// 第二次 执行#3 price+=0.1 得price=0.2 再执行#2 funds>=price 此时funds=0.9,price=0.2,结果为true 进入for循环 funds-=price 得funds=0.7
// 第三次 执行#3 price+=0.1 得price=0.30000000000000004 在执行#2 funds>=price 此时funds=0.7,price=0.30000000000000004(开始误差了) 结果为true 进入for循环 funds-=price 得 funds=0.3999999999999999
// 第四次 执行# price+=0.1 得price=0.4 再执行#2 funds>=price 此时funds=0.3999999999999999 ,price=0.4,结果为false 不进入循环体 所以itemBought结果为3
//#解释1
System.out.println(itemBought+" items bought.");
System.out.println("change:$"+funds);
}
对于误差解决办法是使用 BigDecimal,int或long进行货币计算,int和long涉及数值大小,
BigDecimal则用于对精度要求比较高的场合,下面我们使用BigDecimal写了个简单代码
package com.hra.riskprice;
import com.hra.riskprice.SysEnum.Factor_Type;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.math.BigDecimal;
import java.util.*;
@SpringBootApplication
public class RiskpriceApplication {
public static void main(String[] args) {
final BigDecimal TEN_CENTS=new BigDecimal(".10");
int itemBought=;
BigDecimal funds=new BigDecimal("1.00");
for(BigDecimal price=TEN_CENTS;funds.compareTo(price)>=;price=price.add(TEN_CENTS)){
funds=funds.subtract(price);
itemBought++;
}
System.out.println(itemBought+" items bought.");
System.out.println("change:$"+funds);
}
}
for循环什么执行的就不分析了,自己调试下加深映像就好,通常for的执行顺序都是如此,感谢观摩
java 小心使用float和double他可能不如你所想的更多相关文章
- 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 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 i ...
- 不要在精确计算中使用float和double类型
http://blog.csdn.net/androiddevelop/article/details/8478879 一 问题描述 float和double类型不能用于精确计算,其主要目的是为了科 ...
- Char、float、Double、BigDecimal
Char初识 char: char类型是一个单一的 16 位 Unicode 字符 char 在java中是2个字节("字节"是byte,"位"是bit ,1 ...
- java float、double精度研究(转)
在java中运行一下代码System.out.println(2.00-1.10);输出的结果是:0.8999999999999999很奇怪,并不是我们想要的值0.9 再运行如下代码:System.o ...
- java.lang基础数据类型boolean、char、byte、short、int、long、float、double (JDK1.8)
java.lang.Boolean public static int hashCode(boolean value) { return value ? 1231 : 1237; } JDK 1.8新 ...
- Java中 float、double使用注意问题
在java中运行一下代码 System.out.println(2.00-1.10);输出的结果是:0.8999999999999999很奇怪,并不是我们想要的值0.9 再运行如下代码:System. ...
随机推荐
- JAVA web端JS下载excel文件
JSP代码如下: JSP端引入jquery.easyui.min.js库: <script type="text/javascript" src="<c:ur ...
- 读完这个我懂了JNDI
转载自:http://私塾在线/forum/blogPost/list/1186.html NDI 是什么 JNDI是 Java 命名与目录接口(Java Naming and Directory I ...
- 用到的linux命令
1.修改文件权限 chmod 777 文件路径 修改文件下所有文件权限 chmond -R 777 文件路径 2.修改文件 (保存文件的方法,在命令行窗口 shift+:换出底部命令行, q表示退出, ...
- maven打包时报错:-source 1.5 中不支持 diamond 运算符
报错现象: 解决方法: 在pom文件中加入下面依赖 <build> <plugins> <plugin> <groupId>org.apache.mav ...
- 涂抹mysql笔记-安装mysql
1.mysql安装:(1)RPM安装:rpm -ivh xxx 建议安装三个:MySQL-server-VERSION.PLATFORM-cpu.rpmMySQL-client-VERSION.PLA ...
- [SQL]T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)
T-Sql 递归查询(给定节点查所有父节点.所有子节点的方法) -- 查找所有父节点with tab as( select Type_Id,ParentId,Type_Name from Sys_ ...
- django补充和form组件
Model常用操作: - 参数:filter - all,values,values_list [obj(id,name,pwd,email),obj(id,name,pwd,email),] mod ...
- Android 开发 VectorDrawable 矢量图 (三)矢量图动画
VectorDrawable 矢量图 三部曲: Android 开发 VectorDrawable 矢量图 (一)了解Android矢量图与获取矢量图 Android 开发 VectorDrawabl ...
- scrapy-logging
import logging logger = logging.getLogger(__name__) # 当前文件位置 logger.warning('haha') # debug info 201 ...
- json字符串装List<Object>
List<SearchParam> ts = (List<SearchParam>) JSONArray.parseArray(jsonStr, SearchParam.cla ...