primitives

float

In this example we shall show you how to use a float type in Java. The float data type is a single-precision 32-bit IEEE 754 floating point. Use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead.

    To use a variable of float type one should type the float keyword in a variable,

as described in the code snippet below.
 
 

package com.javacodegeeks.snippets.basics;

public class FloatExample {
	
	public static void main(String[] args) {
		
		float f1 = 422.27f;
		float f2 = (float)1234/33;
		
		System.out.println("Value of float f1 is: " + f1);
		System.out.println("Value of float f2 is: " + f2);
		
	}

}

Output:

Value of float f1 is: 422.27
Value of float f2 is: 37.39394

  
This was an example of how to use a float type in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button