How to Convert Float to String in Java

In this tutorial, we will learn how to convert primitive type float or wrapper Float class object to string in Java.

Sometimes, we need to convert values from one data type to another, under certain situation.

For example, if we need to append a float value to a JTextArea, first we need to convert it to a string before appending to JTextArea.

Therefore, this tutorial will cover six different ways to convert a primitive float or Float wrapper class to string in Java.

Different ways for converting float to string are as:

  • Convert using Long.toString(long)
  • Convert using String.valueOf(long)
  • Converting using String.format()
  • Converting using DecimalFormat
  • By using StringBuffer
  • By using StringBuffer

Let’s understand all the six ways one by one with example programs.

Converting Float to String in Java using String.valueOf()


To convert a primitive type float value to string in Java, we use valueOf() method of the String class. The method String.valueOf() converts float to string. It is a static utility method of the String class.

Therefore, we do not need to create an object to calling this method. We can simply call by using its class name. The general signature of valueOf() method is as below:

public static String valueOf(float f)

This method accepts a float numeric value that needs to be converted and returns the string representation of the float value. Let’s create a simple program to convert a primitive float to string value in Java.

Program code 1:

// Java Program to demonstrate the conversion of float into string using String.valueOf() method.
package javaConversion;
public class FloatToStringConversion {
public static void main(String[] args) 
{
 float num = 59.89f;	
// Call valueOf() method of String class for converting float to string. 
   String str = String.valueOf(num);  
   System.out.println(num + 200); // 259.89 because + is binary plus operator .
   System.out.println(str + 200);// 59.89200 because + is string concatenation operator.
 }
}
Output:
      259.89
      59.89200

Converting Float to String in Java using Float.toString(float)


The float wrapper class in Java also provides a toString() method that converts float numeric value to String. The toString() method of Float class is a static utility method. It returns a string object representing the specified float numeric value.

The general signature of this method is as below:

public static String toString(float f)

Let’s create a simple Java program to convert a primitive float value to string in java using Float.toString() method.

Program code 2:

// Java Program to demonstrate the conversion of float into string using Float.toString().
package javaConversion;
public class FloatToStringConversion {
public static void main(String[] args) 
{
 float num = 59.89f;	
// Call toString() method of Float class for converting float to string. 
   String str = Float.toString(num);
   System.out.println(num + 200); // 259.89 because + is binary plus operator .
   System.out.println(str + 200);// 59.89200 because + is string concatenation operator.
 }
}
Output:
      259.89
      59.89200

Convert Float to String using String.format()


The String.format() method of String class formats given arguments into String. Java has introduced it since JDK 1.5. This is a new alternative way for converting a Float class object to a string object.

The purpose of this method is to format a string. The general syntax for this method is as below:

public static String format(String format, Object... args)

Let’s create a Java program to convert a primitive float to string in Java using format() method of String class.

Program code 3:

// Java Program to demonstrate the conversion of float numeric value into string using format().
package javaConversion;
public class FloatToStringConversion {
public static void main(String[] args) 
{
 float num = 59.89f;	
 String str = String.format("%f", num);  
 System.out.println(str); 
 System.out.println(str + 200);// 59.89200 because + is string concatenation operator.
 }
}
Output:
      59.889999
      59.889999200

Converting Float to String using DecimalFormat


DecimalFormat is a concrete subclass of NumberFormat class that formats a number to a string representation. It provides a lot of features designed to parse and format numbers.

We can use it to format a number to a string representation using a certain pattern. Here’s an example program using DecimalFormat.

Program code 4:

// Java Program to demonstrate the conversion of float into string using DecimalFormat.
package javaConversion;
import java.text.DecimalFormat;
public class FloatToStringConversion {
public static void main(String[] args) 
{
 float num = 59.89f;	
 DecimalFormat numberFormat = new DecimalFormat("##,###");
 String str = numberFormat.format(num);        
 System.out.println("Number to be converted is: " + num);
 System.out.println("String representation of 59.89f is: " + str);
 System.out.println(str + 200);// 59.89200 because + is string concatenation operator.
 }
}
Output:
      Number to be converted is: 59.89
      String representation of 59.89f is: 60
      60200

Convert Float to String in Java using StringBuffer


StringBuffer is a class that concatenates multiple values into a single string. It is thread-safe, but it is slower.

The StringBuffer object represents a String object that we can change and treat as an array with a sequence of characters.

To add a new value to the end of the string, StringBuffer instance implements the append() method. At the end, call the toString() method, in order to take the string representation of the data.

Here is an example program based on the conversion of float to String in Java.

Program code 5:

// Java Program to demonstrate the conversion of float into string using StringBuffer.
package javaConversion;
public class FloatToStringConversion {
public static void main(String[] args) 
{
 float num = 59.89f;	
// Create StringBuffer object.  
   StringBuffer sb = new StringBuffer();
   sb.append(num);
   String str = sb.toString();
   System.out.println("String representation: " + str);
   System.out.println(str + 200);// 59.89200 because + is string concatenation operator.
 }
}
Output:
      String representation: 59.89
      59.89200

You can also write the following code to converting a primitive float type to string in Java.

String str = new StringBuffer().append(59.89).toString();
System.out.println("String representation: " + str);

Converting Float to String using StringBuilder


Let’s create a Java program to convert float numeric value to string representation using StringBuilder class. We can change (or modify) the content of StringBuilder, without creating a new StringBuilder object.

Program code 6:

// Java Program to demonstrate the conversion of float into string using StringBuilder.
package javaConversion;
public class FloatToStringConversion {
public static void main(String[] args) 
{
 float num = 59.89f;	
// Create StringBuffer object.  
  StringBuffer sb = new StringBuffer();
  sb.append(num);
  String str = sb.toString();
  System.out.println("String representation: " + str);
  System.out.println(str + 200);// 59.89200 because + is string concatenation operator.
 }
}
Output:
     String representation: 59.89
     59.89200

In this tutorial, you learned how to convert a primitive type float to string in Java in several ways. Hope that you will have understood and practiced all programs based on the conversion of float to string in Java.
Thanks for reading!!!
Next ⇒ Convert String to Double in Java⇐ Prev Next ⇒