Java convert float to String – Format to N decimal points

Learn to convert float value to String using Float.toString() and String.valueOf() methods. Learn to format float to string to n decimal points.

1. Java convert float to String

Use any of given two methods for converting a java float value to String.

1.1. Convert float to String – Float.toString(float f)

This method returns a string representation of the float value passed as argument.

float pi = 3.1415927f;
String piString = Float.toString(pi);


Program output:


1.2. Convert float to String – String.valueOf(float f)

The valueOf() method takes an float value as argument and return the equivalent string representation. The representation is exactly the one returned by the Float.toString() method.

String.valueOf() method definition
public static String valueOf(float f) 
    return Float.toString(f);
String.valueOf() example
float pi = 3.1415927f;
String piString = String.valueOf(pi);


Program output:


2. Java format float to String

Use NumberFormat.format(float) method to format float value to string in predefined format – such as set decimal places in formatted string.

For example, we can format float to 2 decimal points as in given example.

float pi = 3.1415927f;

NumberFormat formatter = new DecimalFormat("0.00");

String formmatedFloatValue = formatter.format(pi);

System.out.println( formmatedFloatValue );



Program output:


3. Java convert String to float

Use Float.parseFloat(string) method to convert float value to string.

String piString = "3.1415927";

float pi = Float.parseFloat(piString);


Program output:


In this article, we learned to get float value from string in Java. You may be interested in reading about the correct way to compare two floats.

Happy Learning !!

Leave a Reply

Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.

Our Blogs

REST API Tutorial