Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Sunday, November 7, 2021

Java String to Float & Float to String Examples

1. Overview

In this tutorial, We'll learn how to convert String to Float value and Float to String value in java programming.

This can be done in several ways by using simple java api methods and also using third party api methods.

Java String to Float & Float to String Examples


2. Java String to Float Conversion


Converting string to float is done in mainly three ways using following methods.

All of these methods works for negative values alos.

  • Float.parseFloat()
  • Float.valueOf()
  • Float(String) constructor

2.1 Java String to Float Using Float.parseFloat()


Float.parseFloat() method is a static method from Float class and parseFloat() method returns primitive float value.

Look at the below example.
package com.javaprogramto.programs.strings.tofloat;

public class StringToFloatExample1 {

	public static void main(String[] args) {

		String floatValueInString1 = "789.123F";

		float floatValue1 = Float.parseFloat(floatValueInString1);

		System.out.println("Float value in String : " + floatValueInString1);
		System.out.println("String to Float value : " + floatValue1);

		String floatValueInString2 = "123.45";

		float floatValue2 = Float.parseFloat(floatValueInString2);

		System.out.println("Float value in String : " + floatValueInString2);
		System.out.println("String to Float value : " + floatValue2);
	}
}

Output
Float value in String : 789.123F
String to Float value : 789.123
Float value in String : 123.45
String to Float value : 123.45

String value can take the F value at the end of the string to denote the value is float. If F is present in the middle of the string or any other alphabet then it will throw NumberFormatException.

2.2 Java String to Float Using Float.valueOf()


Float.valueOf() method takes the String as input and returns Float wrapper instance.

Below is the example.
package com.javaprogramto.programs.strings.tofloat;

public class StringToFloatExample2 {

	public static void main(String[] args) {

		String floatValueInString1 = "789.123F";

		Float floatValue1 = Float.valueOf(floatValueInString1);

		System.out.println("Float value in String : " + floatValueInString1);
		System.out.println("String to Wrapper Float value : " + floatValue1);

		String floatValueInString2 = "123.45";

		Float floatValue2 = Float.valueOf(floatValueInString2);

		System.out.println("Float value in String : " + floatValueInString2);
		System.out.println("String to Wrapper Float value : " + floatValue2);
	}
}

Output is same as above the section.

valueOf() method internally calls parseFloat() method.


2.3 Java String to Float Using Float Constructor


Float constructor takes argument type of String. But this way is deprecated and suggested to use parseFloat() method.

But using of constructor will produce the same results.
package com.javaprogramto.programs.strings.tofloat;

public class StringToFloatExample3 {

	public static void main(String[] args) {

		String floatValueInString1 = "789.123F";

		Float floatValue1 = new Float(floatValueInString1);

		System.out.println("Float value in String : " + floatValueInString1);
		System.out.println("String to Float value using constructor: " + floatValue1);

		String floatValueInString2 = "123.45";

		Float floatValue2 = new Float(floatValueInString2);

		System.out.println("Float value in String : " + floatValueInString2);
		System.out.println("String to Float value using constructor: " + floatValue2);
	}
}


3. Java Float To String Conversion


Next, Let us see how to convert the Float to String value.

Float to String conversions part is already discussed in detail here.

Complete example of float to string conversion with primitive and wrapper objects.
package com.javaprogramto.programs.strings.tofloat;

public class FloatToStringExample {

	public static void main(String[] args) {

		// example 1 - valueOf()
		float primitiveFloat1 = 456.78f;

		String floatInString = String.valueOf(primitiveFloat1);

		System.out.println("Pritive float value 1 : " + primitiveFloat1);
		System.out.println("Float to String 1 : " + floatInString);

		// example 2 - valueOf()

		Float primitiveFloat2 = 456.78F;

		String floatInString2 = String.valueOf(primitiveFloat2);

		System.out.println("Pritive float value 2 : " + primitiveFloat2);
		System.out.println("Float to String 2 : " + floatInString2);

		// Example 3 - toString()

		float primitiveFloat3 = new Float(12367.987);

		String floatInString3 = Float.toString(primitiveFloat3);

		System.out.println("Pritive float value 3 : " + primitiveFloat3);
		System.out.println("Float to String 3 : " + floatInString3);
	}
}


Output:
Pritive float value 1 : 456.78
Float to String 1 : 456.78
Pritive float value 2 : 456.78
Float to String 2 : 456.78
Pritive float value 3 : 12367.987
Float to String 3 : 12367.987

4. Conclusion


In this article, we've seen how to convert String to Float and vice versa with examples.

When a String cannot be converted successfully then java throws runtime NumberFormatException.

Additionally, you can use apache commons beans utils FloatConverter class to convert String to Float.


No comments:

Post a Comment

Please do not add any spam links in the comments section.