Question

For all those playing along at home I'm reading Absolute Java 5th Edition by Walter Savitch (Chapter 2 Page 66).

The Expression

double d = 12345.123456789;
System.out.printf("START%12.5e END %n", d);

The Answer

START 1.23451e+04END

I understand the basic principles of this printf method's arguments for example I know the '%' represents the start of the parameters. '12' is the number of spacing, '.5' is the times the decimal point will move.... I see the decimal point has moved 4 places towards the left... can someone explain the principles of e-notation. Also how this expression came to this answer =).

As far as e-notation go's its meant to be scientific notation;

So like 5.89e-4 would mean 0.000589 (move the decimal place if minus left if not move the decimal place right).

Was it helpful?

Solution

The "e" is "exponential".

In the format expression:

The 12 is the (minimum) width of the the displayed number presentation. The 5 is the number of decimal places of precision.

The format itself is scientific notation. The number is always normalized to something between 1 and 10, and the "+04" in the printed expression is the power of 10 to multiply the number portion by.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top