$show=/label

Java String to Float & Float to String Examples

SHARE:

A quick guide to convert the String to Float and Float to String objects in java with 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.


COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,21,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,32,Collector,1,Command Line,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,135,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,7,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,127,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,113,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,29,String,61,String Programs,28,String Revese,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,24,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Java String to Float & Float to String Examples
Java String to Float & Float to String Examples
A quick guide to convert the String to Float and Float to String objects in java with examples.
https://1.bp.blogspot.com/-DmPPGZOst70/YYfSEh-Sj3I/AAAAAAAADao/OKhXY_DkUo0N63m_nhuTyZsAN0gzkzFwwCLcBGAsYHQ/w400-h285/Java%2BString%2Bto%2BFloat%2B%2526%2BFloat%2Bto%2BString%2BExamples.png
https://1.bp.blogspot.com/-DmPPGZOst70/YYfSEh-Sj3I/AAAAAAAADao/OKhXY_DkUo0N63m_nhuTyZsAN0gzkzFwwCLcBGAsYHQ/s72-w400-c-h285/Java%2BString%2Bto%2BFloat%2B%2526%2BFloat%2Bto%2BString%2BExamples.png
JavaProgramTo.com
https://www.javaprogramto.com/2021/11/java-string-float-conversion.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2021/11/java-string-float-conversion.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content