Java Program to Convert a Float value to String
Last Updated :
07 Feb, 2023
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.
Approaches
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
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 to the string variable.
Below is the implementation of the above approach:
Java
class GFG {
public static String convertFloatToString( float floatValue)
{
String stringValue = "" + floatValue;
return (stringValue);
}
public static void main(String[] args)
{
float floatValue = 1f;
String stringValue;
stringValue = convertFloatToString(floatValue);
System.out.println(
floatValue + " after converting into string = "
+ stringValue);
}
}
|
Output
1.0 after converting into string = 1.0
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach 2 – Using String.valueOf() method
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
class GFG {
public static String
convertFloatToString( float floatValue)
{
return String.valueOf(floatValue);
}
public static void main(String[] args)
{
float floatValue = 1 ;
String stringValue;
stringValue = convertFloatToString(floatValue);
System.out.println(
floatValue
+ " after converting into string = "
+ stringValue);
}
}
|
Output
1.0 after converting into string = 1.0
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach 3 – Using Float.toString() method
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
import java.util.*;
class GFG {
public static String
convertFloatToString( float floatValue)
{
return Float.toString(floatValue);
}
public static void main(String[] args)
{
float floatValue = 1 ;
String stringValue;
stringValue = convertFloatToString(floatValue);
System.out.println(
floatValue
+ " after converting into string = "
+ stringValue);
}
}
|
Output
1.0 after converting into string = 1.0
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...