java使用数字浮点数 [英] java using number float

查看:76
本文介绍了java使用数字浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个浮号

I have tow number float

float a = 3741164032f;
float b = 3741163983f;
float c = a-b;
System.out.printf("%f",a);



而且我不知道为什么c = 0.



And i don''t unknow why c =0.

推荐答案

float数据类型精确到24位,大约是大约小数点后7位.因此,您的两个电话号码均为3.741164e9.
如果使用double,此问题将得到缓解.如果需要准确,更好的方法是使用long [^ ].
The float data type is accurate to 24 bits which is about 7 decimal places. So both of your numbers will be 3.741164e9.
If you used double this problem would be alleviated. Better if you need to be exact would be to use either long or BigDecimal[^].


很遗憾,这是浮点类型的精度.为什么不使用整数类型作为示例?它将为您带来预期的结果.

—SA
Unfortunately, this is the accuracy of float type. Why not using integer type for your example? It would give you the result you expected.

—SA


这篇关于java使用数字浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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