Java Program to Convert String to Float Value
Last Updated :
05 Aug, 2021
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 to float values.
Method 1: Using parseFloat()
The parseFloat() method in Float Class is a built-in method in Java that returns a new float initialized to the value represented by the specified String, as done by the valueOf method of class Float.
Syntax:
public static float parseFloat(String s) ;
Parameters: It accepts a single mandatory parameter s which specifies the string to be parsed.
Return type: It returns e float value represented by the string argument.
Exceptions:
- NullPointerException: When the string parsed is null
- NumberFormatException: When the string parsed does not contain a parsable float
Example 1
Java
// Java Program to Convert String to Float Value by
// Implementing parseFloat() method of Float class
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Input string
String str = "100";
// Returning the float value
// represented by the string argument
float val = Float.parseFloat(str);
// Prints the float value of the string
System.out.println("String is converted to float "
+ val);
}
}
OutputString is converted to float 100.0
Example 2: To show unsuccessful conversion
Java
// Java Program to Convert String to Float Value by
// Implementing parseFloat() method of Float class
// Where Exception Message is thrown
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Method 1
// To convert string to float
public static void convertStringToFloat(String str) {
float floatValue;
// Try block to check for exceptions
try {
// Convert string to float
// using parseFloat() method
floatValue = Float.parseFloat(str);
// Print the expected float value
System.out.println(
str
+ " after converting into float = "
+ floatValue);
}
// Catch block to handle the exception
catch (Exception e) {
// Print the exception message
// using getMessage() method
System.out.println(
str
+ " cannot be converted to float: "
+ e.getMessage());
}
}
// Method 2
// Main driver code
public static void main(String[] args) {
// Declaring and initializing custom strings values
String str1 = "";
String str2 = null;
String str3 = "GFG";
// Convert string to float
// using parseFloat() method
convertStringToFloat(str1);
convertStringToFloat(str2);
convertStringToFloat(str3);
}
}
Output:
Method 2: Using valueof() method
The valueOf() method of the Float class converts data from its internal form into human-readable form. The valueOf() method converts data from its internal form into a human-readable form. It is a static method that is overloaded within a string for all of Java’s built-in types so that each type can be converted properly into a string.
It is called when a string representation of some other type data is needed-for example during concatenation operation.you can call this method with any data type and get a reasonable String representation valueOf() returns java.lang.Integer, which is the object representative of the integer Few forms of valueOf()
Syntax:
Float.valueOf(str);
Returns:
- It returns string representation of given value
- valueOf(iNum); // Returns the string representation of int iNum.
- String.valueOf(sta); // Returns the string representation of the boolean argument.
- String.valueOf(fNum); // Returns the string representation of the float fnum.
- String.valueOf(data, 0, 15); // Returns the string representation of a specific subarray of the chararray argument.
- String.valueOf(data, 0, 5); // Returns the string of charArray 0 to 5
- String.valueOf(data, 7, 9); // Returns the string of charArray starting index 7 and total count from 7 is 9.
Example
Java
// Java Program to Convert String to Float Value
// Using valuesOf() method
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Method 1
// To convert String to Float
public static float convertStringToFloat(String str)
{
// Convert string to float
// using valueOf() method
return Float.valueOf(str);
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Custom input string value
String stringValue = "1.0";
// Expected float value
float floatValue;
// Converting string to float
floatValue = convertStringToFloat(stringValue);
// Printing the expected float value
System.out.println(
stringValue + " after converting into float = "
+ floatValue);
}
}
Output1.0 after converting into float = 1.0
Similar Reads
Java Program to Convert a Float value to String
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
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 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 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
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 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
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