十进制后允许的最大数字为双倍 [英] Maximum allowed digits after decimal for double

查看:177
本文介绍了十进制后允许的最大数字为双倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想知道在十进制数
之后允许使用多少个数字,用于原始双数据类型java,没有实际获得
四舍五入。

I want to know how many number of digits are allowed after decimal point for primitive double datatype in java, without actually getting rounded off.


推荐答案

从字面上来说,是零。小数点后至少有一位数字的小数部分在转换为四舍五入。例如,小数分数 0.1 将四舍五入为 0.1000000000000000055511151231257827021181583404541015625 转换为双倍。

Taken literally, the number is 0. Most decimal fractions with at least one digit after the decimal point get rounded on conversion to double. For example, the decimal fraction 0.1 is rounded to 0.1000000000000000055511151231257827021181583404541015625 on conversion to double.

问题是double是一个二进制浮点系统,大多数小数部分不能再被精确地表示,而1/3可以精确地表示为具有有限数字有效数字的小数部分。

The problem is that double is a binary floating point system, and most decimal fractions can no more be exactly represented in than 1/3 can be exactly represented as a decimal fraction with a finite number of significant digits.

如已经建议的那样,如果真正精确地表示小数部分很重要,请使用 BigDecimal

As already recommended, if truly exact representation of decimal fractions is important, use BigDecimal.

对于正常数字,原始double具有53个有效位,约15.9个十进制数字。如果非常非常接近就够了,你可以使用double。

The primitive double has, for normal numbers, 53 significant bits, about 15.9 decimal digits. If very, very close is good enough, you can use double.

这篇关于十进制后允许的最大数字为双倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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