Type of Fractional Numeric Data Types used in Java Programming

The integral numbers covered in earlier article could store only whole numbers i.e. they could not have decimal values. Fractional data types can store fractional numbers i.e. numbers having decimal points. There are also called floating-point data types.

Following list displays various floating-point data types available in Java, along with their ranges.

  • Float (Size – 32 bits), Single precision having range -3.4E+38 to +3.4E +38. This data type precision up to 6 digits like temperatures, currency, and percentage or may be some length.
  • Double (Size - 64 bits), Double precision having range -17E+308 to 1.7E+308. This data type precision up to 15 digits like large numbers or high precision, such as for astronomy or subatomic physics.

Now depending upon your requirements for the decimal precision of the data, you can select from float or double types.

For example, you can store the salary values of an employee using a data variable of the float type. To store decimal values with a higher degree of precision, such as values calculated using the functions, sin() and sqrt(), you see the double data type.

By default, Java treats fractional numbers as of double data type e.g. if you write 0.123, it will treated as a double value. To explicitly specify it to be of float type, use suffix F or f i.e. write 1.234f or 1.234F. Similarly if you use suffix d or D, it means double i.e., 1.234D or 1.234d means it is a double value.


Post a Comment