The number 3.03
, when it is stored in a double
, is stored as 8 bytes, 64 bits. This is the whole value:
01000000_00001000_00111101_01110000_10100011_11010111_00001010_00111101
Those bits include the number's mantissa, sign and exponent. There is nothing in it that indicates format. There is no place for that in 8 bytes of information.
When you print a double using, for example, System.out.print
, it's the print
method that decides how many digits to print, based on the ability to distinguish this number from the nearest double numbers. Thus, print
decides on the format - it's not stored in the number.
But you can decide which format you want to print a number in, by using a Formatter
(e.g. System.out.printf
, which uses a Formatter
) or a DecimalFormat
.
You decide on the format in this case. It has nothing to do with the number. For example, using
System.out.printf("%7.3f%n",a);
you'll get the result
3.030
Using it on your b
will result in
56.093
So you can dictate the format, but it's not decided by the number. It's decided by the "%7.3f"
format.