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 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 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 Char to Int Given a char value, and our task is to convert it into an int value in Java. We can convert a Character to its equivalent Integer in different ways, which are covered in this article.Examples of Conversion from Char to Int:Input : ch = '3'Output : 3Input : ch = '9'Output : 9The char data type is a s
4 min read
Convert String to Float in Java To convert a String to a Float in Java, there are several ways including built-in methods and handling potential exceptions for invalid inputs.Example: For this conversion, we will use the most common and efficient method i.e. parseFloat() method. This method converts a string into a primitive float
2 min read
Java Program to Convert Int to Double Java data types can be categorized as primitive and non-primitive. Primitive data types contain a single value, whereas non-primitive data types contain an address of the variable value. Java supports 7 primitive data types - boolean, byte, char, short, int, long, float, and double. These data types
4 min read
How to Convert a String value to Short value in Java with Examples Given a String "str" in Java, the task is to convert this string to short type. Examples: Input: str = "1" Output: 1 Input: str = "3" Output: 3 Approach 1: (Naive Method) One method is to traverse the string and add the numbers one by one to the short type. This method is not an efficient approach.
2 min read