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
// 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 + 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);
}
}
Output1.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
// 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);
}
}
Output1.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
// Java Program to convert float value to String value
import 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);
}
}
Output1.0 after converting into string = 1.0
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Java Program to Convert String to Float Value
Given a string âstrâ in Java, the task is to convert this string to float type. Methods: This can be done by two methods as listed below: Using parseFloat() Method Using valueOf() Method Let us discuss above two ways of implementing the methods to get a fair understanding of the conversion of string
4 min read
Java Program to Convert Date to String
The date type object can be converted to a string in Java using a large variety of in-built classes available in Java. Given a date, we need to convert the date to a given format of the string. Examples: Input: date = â2020-11-13âOutput: 2020-11-13 Input: date = â2020-12-14âOutput: 2020-12-14 Method
3 min read
Java Program to Convert Double to String
The primary goal of double to String conversion in Java is to store big streams of numbers that are coming where even data types fail to store the stream of numbers. It is generically carried out when we want to display the bigger values. In this article, we will learn How to Convert double to Strin
3 min read
Java Program to Convert Boolean to String
In Java programming, you might come across the need to convert a simple "true" or "false" boolean value into a String. It may seem like a challenging task, but fear not! In this article, You will explore some methods to convert a boolean value to a string in Java Method for Boolean to String Convers
4 min read
Java Program to Convert String to boolean
In Java, to convert a string to a Boolean, we can use Boolean.parseBoolean(string) to convert a string to a primitive Boolean, or Boolean.valueOf(string) to convert it to a Boolean object. The Boolean data type only holds two possible values which are true and false. If the string equals "true" (ign
3 min read
Java Program to Convert String to Integer Array
In Java, we cannot directly perform numeric operations on a String representing numbers. To handle numeric values, we first need to convert the string into an integer array. In this article, we will discuss different methods for converting a numeric string to an integer array in Java.Example:Below i
3 min read
Java Program to Convert Long to String
The long to String conversion in Java generally comes in need when we have to display a long number in a GUI application because everything is displayed in string form. In this article, we will learn about Java Programs to convert long to String. Given a Long number, the task is to convert it into a
4 min read
Java Program to Convert String to Long
To convert a String to Long in Java, we can use built-in methods provided by the Long class. In this article, we will learn how to convert String to Long in Java with different methods. Example:In the below example, we use the most common method i.e. Long.parseLong() method to convert a string to a
3 min read
Java Program to Convert String to Object
In-built Object class is the parent class of all the classes i.e each class is internally a child class of the Object class. So we can directly assign a string to an object. Basically, there are two methods to convert String to Object. Below is the conversion of string to object using both of the me
2 min read
Java Program to Convert InputStream to String
Read and Write operations are basic functionalities that users perform in any application. Every programming language provides I/O streams to read and write data. The FileInputStream class and FileOutputStream class of Java performs I/O operations on files. The FileInputStream class is used to read
4 min read