About Ilias Tsagklis

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

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.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of