Related Articles

# Java Program to Illustrate the Usage of Floating

• Last Updated : 07 Sep, 2021

The floating-point number are those number which needs fractional precision that is the number that can be in the fraction. There many more mathematical calculations in which floating type is involved. For e.g. finding the square root of a number up to certain decimal values finding the cube root of the number, finding roots of quadratic equation, and calculation involving trigonometric functions like sin cos tan.

There are two types of floating-point data type

• Float
• Double

Float: It is a single-precision value that has 32 bits in storage. and this single precision is faster and does take less size compared to double precision.

For java variables, we can use float while declaring or initializing for expected value to be fractional. The default value in java is 0.0f and its size is 4 bytes. Float in java can have negative values.

The correct ways and incorrect ways of defining java floating-point.

1. float a1=10.57f which is equal to 10.57
2. float a2 =10f it is equal to 10.0
3. float a3=9.58 it will give an error.

Here we concluded that if we are initializing any value with float it ends with f then it is correct otherwise it will throw an error.

Printing float values:

## Java

 `// Java Program to Illustrate the Usage of Floating``import` `java.io.*;` `public` `class` `GFG {` `    ``public` `static` `void` `main(String[] args)``    ``{` `        ``// initialized two float variables with``        ``// the least and max value of float` `        ``float` `a1 = ``1``.40129846432481707e-45f;``        ``float` `a2 = ``3``.40282346638528860e+38f;` `        ``// printed the value of a1 and a2``        ``System.out.println(``"Start range: "` `+ a1);``        ``System.out.println(``"End range: "` `+ a2);``    ``}``}`

Output
```Start range: 1.4E-45
End range: 3.4028235E38```

Multiplication of float values:

## Java

 `// Java Program to Illustrate the Usage of Floating``import` `java.io.*;` `public` `class` `GFG {` `    ``public` `static` `void` `main(String[] args)``    ``{` `        ``// initialized two float variables a1 and a2.``        ``// declared n3 which will contain``        ``// the output of a1 * a2.` `        ``float` `a1 = ``10``.89f;``        ``float` `a2 = ``7``.43f;``        ``float` `a3;` `        ``// multiplied n1 and n2 and stored it in a3``        ``a3 = a1 * a2;` `        ``// printed the value of a3``        ``System.out.println(``"The result of n1 x n2 is: "``                           ``+ a3);``    ``}``}`

Output
`The result of n1 x n2 is: 80.912704`

Providing value in Scientific notation

## Java

 `// Java Program to Illustrate the Usage of Floating``import` `java.io.*;` `public` `class` `GFG {` `    ``public` `static` `void` `main(String[] args)``    ``{` `        ``// initialized two float variables a1 and a2.``        ``// a1 has simple value of float type and a2``        ``// has the equivalent scientific notation.` `        ``float` `a1 = ``283``.75f;``        ``float` `a2 = ``2``.8375e2f;` `        ``// printed the value of a1 and a2``        ``System.out.println(``"Simple Float: "` `+ a1);``        ``System.out.print(``"Scientific Notation: "` `+ a2);``    ``}``}`

Output
```Simple Float: 283.75
Scientific Notation: 283.75```

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up