Literal Formats for Floating-Point - Example

This section provides a tutorial example on how to use floating-point literals in 2 formats: decimal and hex.

Now let's see a tutorial program example on floating-point literals in decimal and hex formats:

```/* FloatingPointLiteral.java
*/

class FloatingPointLiteral {
public static void main(String[] a) {
java.io.PrintStream o = System.out;
o.println("Decimal Floating-Point Literals:");
o.println("   Base part:     365.242196     = "+365.242196);
o.println("   With exponent: 3.65242196e2   = "+3.65242196e2);
o.println("   Negative exponent:    1.0e-9  = "+1.0e-9);
o.println("   float type:           1.0e-9F = "+1.0e-9F);
o.println("   Using _:     3.65_242_196e2   = "+3.65_242_196e2);

o.println();
o.println("Hex Floating-Point Literals:");
o.println("   1:                     0x1.0p0  = "+0x1.0p0);
o.println("   16:                   0x10.0p0  = "+0x10.0p0);
o.println("   16*16:                0x10.0p0  = "+0x10.0p0);
o.println("   1/16:                  0x0.1p0  = "+0x0.1p0);
o.println("   1/16/16:               0x0.01p0 = "+0x0.01p0);
o.println("   With exponent:     0x1.23456p8  = "+0x1.23456p8);
o.println("   Negative exponent: 0x12345.6p-8 = "+0x12345.6p-8);
o.println("   float type:    0x800000.0p0F = "+0x8000000.0p0F);
o.println("   Using _:      0x80_0000.0p0F = "+0x80_00000.0p0F);
}
}
```

Compile this example program in a command window and run it:

```C:\herong>javac FloatingPointLiteral.java

C:\herong>java FloatingPointLiteral

Decimal Floating-Point Literals:
Base part:     365.242196     = 365.242196
With exponent: 3.65242196e2   = 365.242196
Negative exponent:    1.0e-9  = 1.0E-9
float type:           1.0e-9F = 1.0E-9
Using _:     3.65_242_196e2   = 365.242196

Hex Floating-Point Literals:
1:                     0x1.0p0  = 1.0
16:                   0x10.0p0  = 16.0
16*16:                0x10.0p0  = 16.0
1/16:                  0x0.1p0  = 0.0625
1/16/16:               0x0.01p0 = 0.00390625
With exponent:     0x1.23456p8  = 291.27099609375
Negative exponent: 0x12345.6p-8 = 291.27099609375
float type:    0x800000.0p0F = 1.34217728E8
Using _:      0x80_0000.0p0F = 1.34217728E8
```

Hope you know how to use floating-point literals properly now.

Last update: 2014.