简体   繁体   English

我的双胞胎被改变了吗?

[英]Is my double being altered?

So I have 所以我有

double a= 199812280115

When I 当我

System.out.println(a);

I get 我懂了

1.9981228E13  

This representation of double a does not imply that the value stored has been altered right? double a这种表示形式并不表示存储的值已更改,对吗?

The double value printed in the console has been formated, according to the default toString() vaue, but the value remains the same. 根据默认的toString()值,已格式化控制台中打印的double值,但该值保持不变。 If you wish to print the value "as is", you should use a NumberFormat to format and print. 如果您希望按原样打印值,则应使用NumberFormat进行格式化和打印。

A good way to check if your value is altered is by doing something like this: 检查您的价值是否改变的一个好方法是通过执行以下操作:

double a= 199812280115;
double b= 199812280000;
double c = a - b;
System.out.println(c);

You'll see yourself if your double has lost precision. 您会发现自己是否双精度下降。

YES, the output you see does show that the value has been altered. 是的,您看到的输出确实显示该值已更改。

Note that you see only 8 digits after the decimal point. 请注意,小数点后只能看到8位数字。 The double data type has enough precision for 15-16 digits after the decimal point. double数据类型对于小数点后的15-16位数字具有足够的精度。 The default formatting in Java outputs the least amount digits that are required to distinguish the number from its neighbors so you should see all of the digits. Java的默认格式输出的数字最少,该数字用来区分数字与其相邻数字,因此您应该看到所有数字。

Besides, the exponent is wrong. 此外,指数是错误的。 The expected output is 1.99812280115E11 . 预期输出为1.99812280115E11

You could use 你可以用

System.out.format("%12f", a);

To see the number in its full glory. 看到它的全部荣耀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM