Convert String to Float in Java Last Updated : 22 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Java // Java program to convert String to float import java.util.*; public class StringToFloat { public static void main(String[] args) { String s = "1.6"; // Convert string to float float n = Float.parseFloat(s); System.out.println("" + n); } } Output1.6 Different Approaches to Convert String to Float in Java1. Using Float.valueOf() Another approach to convert a string to float in Java is the Float.valueOf() method of the Float class. This method converts a string into a Float object, which can then be used as a primitive float.Syntax of Float.valueOf() MethodFloat.valueOf(str);Example: Java // Java Program to convert string to float import java.util.*; class GFG { // Function to convert String to Float public static float StringToFloat(String s) { // Convert string to float // using valueOf() method return Float.valueOf(s); } // Driver code public static void main(String[] args) { String s1 = "1.0"; // The expected float value float f; // Convert string to float f = StringToFloat(s1); // Print the expected float value System.out.println(s1+ " after converting into float: " + f); } } Output1.0 after converting into float: 1.0 2. Handling Invalid InputIf the input string cannot be parsed as a float, a NumberFormatException is thrown. The below example shows how to handle this types of errors.Example: Java // Java program to handle invalid input // during String to Float conversion import java.util.*; public class StringToFloat { public static void main(String[] args) { String s = "invalid"; try { float n = Float.parseFloat(s); System.out.println("Converted float value: " + n); } catch (NumberFormatException e) { System.out.println("Error: Invalid input for float conversion"); } } } OutputError: Invalid input for float conversion Comment More infoAdvertise with us Next Article Java Program to Convert a Float value to String C code_r Follow Improve Article Tags : Java Java Programs Java-Data Types Java-Float Java-String-Programs +1 More Practice Tags : Java 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 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 How to Convert a String to an Numeric in Java? In Java programming, there are situations in which we must convert a string of numeric characters into one of the following data types: double, float, long, or int. To execute arithmetic operations and other numerical calculations, this conversion is necessary. We will look at how to convert a strin 2 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 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 Fahrenheit into Celsius In this article, we will discuss how to convert a temperature value from Fahrenheit to Celsius using Java.Note: Fahrenheit and Celsius are units of temperature measurement, denoted by °F and °C, respectively.FormulaCelsius = (Fahrenheit - 32) / 1.8ApproachInitialize the value of Fahrenheit as 50.0 a 1 min read Like