Java Program to Convert a Float value to String

30 Sep, 2021
Given a Float value in Java, the task is to write a Java program to convert this float value to string type.
Examples: 
Input: 1.0
Output: "1.0"

Input: 3.14
Output: "3.14"
Strings – Strings in Java are objects that are supported internally by a char array. Since arrays are immutable, and strings are also a type of exceptional array that holds characters, therefore, strings are immutable as well. 
Float – The float data type is a single-precision 32-bit IEEE 754 floating-point. Its value range is unlimited. Using a float (instead of double) is suggested if you need to save memory in large arrays of floating-point numbers. Its default value is 0.0F.
There are numerous approaches to convert a float value to a String in Java. These are – 
Using + operator
Using String.valueOf() method
Using Float.toString() method
One method is to create a string variable and then append the float value to the string variable. This will directly convert the float value to a string and add it to the string variable. 
Below is the implementation of the above approach:
Java
// Java Program to convert float value to String valueclass GFG {      // Function to convert float value to String value    public static String convertFloatToString(float floatValue)    {        // Convert float value to String value        // using + operator method        String stringValue = "" + floatValue;          return (stringValue);    }      // Driver code    public static void main(String[] args)    {          // The float value        float floatValue = 1f;          // The expected string value        String stringValue;          // Convert float to string        stringValue = convertFloatToString(floatValue);          // Print the expected string value        System.out.println(            floatValue + " after converting into string = "            + stringValue);    }}
1.0 after converting into string = 1.0
The simplest way to do so is using the valueOf() method of the String class in java.lang package. This method takes the float value to be parsed and returns the value in String type from it. 
Syntax: 
String.valueOf(floatValue);
Below is the implementation of the above approach:
Java
// Java Program to convert float value to String valueclass GFG {      // Function to convert float value to String value    public static String    convertFloatToString(float floatValue)    {        // Convert float value to String value        // using valueOf() method        return String.valueOf(floatValue);    }      // Driver code    public static void main(String[] args)    {          // The float value        float floatValue = 1;          // The expected string value        String stringValue;          // Convert float to string        stringValue = convertFloatToString(floatValue);          // Print the expected string value        System.out.println(            floatValue            + " after converting into string = "            + stringValue);    }}
1.0 after converting into string = 1.0
The Float.toString() method can also be used to convert the float value to a String. The toString() is the static method of the Float class.
Syntax:
String str = Float.toString(val);
Below is the implementation of the above approach:
Java
// Java Program to convert float value to String valueimport java.util.*;class GFG {      // Function to convert float value to String value    public static String    convertFloatToString(float floatValue)    {        // Convert float value to String value        // using valueOf() method        return Float.toString(floatValue);    }      // Driver code    public static void main(String[] args)    {          // The float value        float floatValue = 1;          // The expected string value        String stringValue;          // Convert float to string        stringValue = convertFloatToString(floatValue);          // Print the expected string value        System.out.println(            floatValue            + " after converting into string = "            + stringValue);    }}
1.0 after converting into string = 1.0
nishkarshgandhi
Java-Data Types
Java-Float
Java-String-Programs
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.