Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

How to convert Java float to String

In Java, we can convert float into a String in two ways:

1. By using String.valueOf() Method

The valueOf() method is a part of String Class. It is a static method that converts a float into a String value.

Example 1:

Here the float value passed in the method is converted into a String.

public class StudyTonight
{  
	public static void main(String args[])
	{  
		float n = 500.08f;  
		String s = String.valueOf(n);
		System.out.println("The string value is " +s);  
	}
}


The string value is 500.08

2. By Using Float.toString() Method

The toString() method is a part of Float Class. It is a static method that can also be used to convert float value to String.

Example 2:

Here the float value passed in the method is converted into a String.

public class StudyTonight
{  
	public static void main(String args[])
	{  
		float n = 500.08f;  
		String s = Float.toString(n); 
		System.out.println("The string value is " +s);  
	}
}


The string value is 500.08

3. By Using format() Method

We can use format() method of DecimalFormat class to get String value of a float value. Here, a float value is passed in the method and converted into a String formatted up to 2 digits.

import java.text.DecimalFormat;
public class StudyTonight
{  
	public static void main(String args[])
	{  
		float n = 500.0878f;  
		Float f = (float)n;
		String s = new DecimalFormat ("#.00").format (f); //for two places decimal
		System.out.println("String is : "+s);
	}
}


String is : 500.09

4. By using the + operator (Concatenation)

We can use + operator to concatenate float value to an empty string and get a string value. Here, a float value is passed in the method and converted into a String using concatenation. See the example below.

public class StudyTonight
{  
	public static void main(String args[])
	{  
		float n = 500.0878f;  
		String str = ""+n; //concat
		System.out.println("String is : " + str);
	}
}


String is : 500.0878