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 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
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
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
Java Program to Convert int to long Given a number of int datatype in Java, your task is to write a Java program to convert this given int datatype into a long datatype. Example: Input: intnum = 5 Output: longnum = 5 Input: intnum = 56 Output: longnum = 56 Int is a smaller data type than long. Int is a 32-bit integer while long is a 6
2 min read
Java Program to Convert Double to Long Given a Double real number. Write a Java program to convert the given double number into a Long (long) in Java. Examples: Input: double = 2545.241 Output: 2545 Input: double = 21.54 Output: 21 Double data type: The double data type is a double-precision 64-bit IEEE 754 floating-point. Its value rang
4 min read