How to Convert a Float value to String value in Java with Examples

Given a Float value in Java, the task is to convert this float value to string type.

Examples:

Input: 1.0
Output: "1.0"

Input: 3.14
Output: "3.14"

Approach 1: (Using + operator)
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 in the string variable.

Below is the implementation of the above approach:

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to convert float value to String value
  
class GFG {
  
    // Function to convert float value to String value
    public static String convert
    FloatToString(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 = 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);
    }
}

chevron_right


Output:

1.0 after converting into string = 1.0

Approach 2: (Using String.valueOf() method)
The simplest way to do so is using valueOf() method of 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:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to convert float value to String value
  
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 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);
    }
}

chevron_right


Output:

1.0 after converting into string = 1.0

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.