String

Java String to Float Example

float is a frequently used data type of 32-bit and it represents decimal numbers. The main difference from double is that it requires smaller size in memory but in some situations (big numbers) doesn’t represent the accurate number. A commonly used procedure in Java is the convertion from string to float.

In this example we shall show you all the possible ways of converting a String to Float in Java.

 
 
 

1. Syntax of the convert operations

There are tree manners to convert a string to float.

  • Float(String s): use of Float constructor where s represents the parsing string.
  • public static float parseFloat(String s): returns a new signed float that is represented by the specified string s.
  • public static Float valueOf(String s): returns a Float object that includes the float value, which has been converted by the given string s.

In is good to mention that both parseFloat() and valueOf() operations throw NumberFormatException and NullPointerException. NumberFormatException is thrown if the string can not be converted to a float number, while NullPointerException is thrown when the string is null.

2. Example of String to Float conversion

Create a new java file with the name StringToFloatClass and paste the following code.

StringToFloatClass.java:

package com.javacodegeeks.basics.stringtofloat;

public class StringToFloatClass {

	public static void main(String[] args) {
		
		String s1 = "1.23456f";
		String s2 = "-2";
		
		try {
			// use of constructor
			Float f1 = new Float("0.123456789012");
			System.out.println("f1 = "+f1.floatValue());
			
			float f2 = Float.valueOf(s1).floatValue();
			System.out.println("f2 = "+f2);
			
			// use of signed number
			float f3 = Float.parseFloat(s2);
			System.out.println("f3 = "+f3);
			
			// parseFloat and valueOf operations return the same result value
			float f4 = Float.parseFloat(s1);
			System.out.println("Comparing f2=f4: result = "+(f2==f4));
			
		} catch(NumberFormatException ex) {
			System.err.println("NumberFormatException "+ ex.getMessage());
		} catch(NullPointerException ex) {
			System.err.println("NullPointerException "+ ex.getMessage());
		}
		
	}

}

Now lets explain the code above. Firstly we use the Float constructor in order to create a float, based on a string. The number is big so, as you can notice in the output below, f1 doesn’t contain the precise value. As we mentioned, valueOf() method returns a Float object so floatValue() is called in order to transform it into a float. In addition, we can convert a string that represents a signed integer number to a float. Finally, we can easily note that parseFloat() and valueOf() operations return the same float value.

Now you can see below the result of the execution.

Output:

f1 = 0.12345679
f2 = 1.23456
f3 = -2.0
Comparing f2=f4: result = true

Download the source code

This was an example of string to float in Java. Download the source code of this example: StringToFloatExample.zip

Katerina Zamani

Katerina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she attends MSc courses in Advanced Information Systems at the same department. Currently, her main academic interests focus on web applications, mobile development, software engineering, databases and telecommunications.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button