0.3*3=0.899999999????

f1555585 2011-09-14 11:07:21
奇怪唉
0.3*3=0.899999999 ?? 不等于0.9
0.2*6=1.200000002 ?? 不等于1.2
自己编的以及从网上下载的java计算器代码运行后都出现类似问题,怎么样规范数据显示?请教!!!!!
...全文
293 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
lucky_heyan 2011-09-14
  • 打赏
  • 举报
回复
这个我也不知道,用过的给个答案项目中用到了
Exc_9521 2011-09-14
  • 打赏
  • 举报
回复
将 0.3*3变成 0.3*3.0 或者将结果截取一位小数
meran 2011-09-14
  • 打赏
  • 举报
回复
传说中的月经贴???
cole_白 2011-09-14
  • 打赏
  • 举报
回复
java用于精确计算会有误差的,sun公司说明了的
sk_up_dyy 2011-09-14
  • 打赏
  • 举报
回复
用BigDecimal
matinghp 2011-09-14
  • 打赏
  • 举报
回复
java数据是用二进制表式的,会有精读损失。。。
lost_guy_in_scut 2011-09-14
  • 打赏
  • 举报
回复

BigDecimal param = new BigDecimal(String.valueOf(0.3));
BigDecimal result = new BigDecimal(3);
System.out.println(result.multiply(param).doubleValue());

的确是好蛋疼的做法~~~
K_THEKING 2011-09-14
  • 打赏
  • 举报
回复
围观!
小绵羊 2011-09-14
  • 打赏
  • 举报
回复
浮点型能表示的小数位有限float是7位,double是14位

http://blog.csdn.net/nujiah001/article/details/6657410
huhk 2011-09-14
  • 打赏
  • 举报
回复
这个和用十进制不能表示1/3的原理是一样的,只能取个近似值。
2399 2011-09-14
  • 打赏
  • 举报
回复
用BigDecimal
beowulf2005 2011-09-14
  • 打赏
  • 举报
回复
哎,这可以说是计算机基础中的基础问题了。
难怪码农现在越来越不值钱了。
原来缘来 2011-09-14
  • 打赏
  • 举报
回复
The elements of the types float and double are those values that can be represented using the IEEE 754 32-bit single-precision and 64-bit double-precision binary floating-point formats, respectively.

The details of proper input conversion from a Unicode string representation of a floating-point number to the internal IEEE 754 binary floating-point representation are described for the methods valueOf of class Float and class Double of the package java.lang.

//推荐用BigDecimal
liyangyun1986 2011-09-14
  • 打赏
  • 举报
回复
double和float的精度问题
24K純帥 2011-09-14
  • 打赏
  • 举报
回复
精度问题
cherry 2011-09-14
  • 打赏
  • 举报
回复
0.3与3不是一种类型,需要类型转换,所以就会出现这种情况了
mrccy 2011-09-14
  • 打赏
  • 举报
回复
需要做精度处理
clementxr 2011-09-14
  • 打赏
  • 举报
回复
需要精确计算的时候把数字类型转换成BigDecimal运算

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧