Decimal literals float, double – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #JAVA#3804    siteicon   siteicon  

Decimal literals float, double

Use of decimal/fractional/real literals in its different size as float and double.

Learning Objectives

  • Decimal literal in float and double with its variation of hexadecimal and exponent notation.

Source Code

TC++ #3804

Source Code

class DecimalLiterals
{
public static void main(String[] args)
{
float a=22.345f;
double b=22.345;
double c=0x22.345P0;
double d=22.345E2;
double e=22.345E-2;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
}
}

Test it !

Run Output

22.345
22.345
34.204345703125
2234.5
0.22345

Code Understanding

float a=22.345f;
The value 22.345f here is a float type literal constant. f is suffixed to differentiate it from double. If you do not suffix it is treated as double value and you can not store it in a float type. Float needs 32 bits of storage

double b=22.345;
Here the value 22.345 is double type literal constant .  Double needs 64 bits of storage.

double c=0x22.345P0;
This is double value shown in hexadecimal notation. This is equivalent to 22 in hex form and then .345 in hex form with Power of 2 as 0 as its multiple.

double d=22.345E2;
This is exponent form which is equivalent to 22.345 x 10^2 which means 2234.5

double e=22.345E-2;
This is negative exponent form which is equilent to 22.345×10^-2 which means 0.22345

System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); System.out.println(e);
Here we print all the decimal equivalents of above.

Notes

  • Exponent notation can allow a value of storage in double data type which is bigger than its no. of bits equivalent.


Suggested Filename(s): DecimalLiterals.java



Share

sunmitra| Created: 11-Mar-2018 | Updated: 11-Mar-2018|






Back