There are three ways to convert a String to float primitive in Java parseFloat(), valueOf() method of Float class, and new Float() constructor. Suppose you have a String that represents a floating-point number e.g. "3.14" which is the value of PIE, you can convert it to float by using any of those three methods. Since String is one of the most prominent data types in Java, you will often find yourself converting String to Int, Double, and other data types and vice-versa. Java designer knows about that and they have made arrangement to carry out this basic task in a predictable and consistent manner.
Once you know the trick to convert String to float, you should be able to convert String to Integer, Double, and Short. BTW, converting String to a byte array is a little bit tricky because String is text data and bytes are binary, so character encoding comes into the picture. If you want to learn more about how to do conversion between String and byte array, see this step by step tutorial for String to a byte array.
String to Float Conversion in Java
As I said, there are three main ways to convert a String to float primitive in Java- Float.parseFloat()
- Float.valueOf()
- new Float()
If you pass an invalid floating point String like something which has alphabets or character other '+' or '-'.
In all cases, autoboxing will take care of converting an object of the Float wrapper class to float primitive variable. One advantage of the second method it reads well and also provides caching of frequently used numbers. Third way internally uses the first method to convert String to float variable and then convert it to Float object.
String concatenation with empty String method works for both float primitive and Float object, but the key is to use empty String first and then put floating-point value letter because then only Java understand that + operator is there for String concatenation and not for adding two floating-point value. If you use Float object then its toString() method is called automatically.
You can also see Core Java Volume 1 - Fundamentals by Cay S. Horstmann to learn more about different static factory methods to convert one type of variable to another primitive or wrapper type in Java.
In all cases, autoboxing will take care of converting an object of the Float wrapper class to float primitive variable. One advantage of the second method it reads well and also provides caching of frequently used numbers. Third way internally uses the first method to convert String to float variable and then convert it to Float object.
Float to String Conversion in Java
On the reverse side also, there are three main ways to convert a float variable to a String in Java. It could be a Float object or float primitive, both will work. Here are the methods to convert a float primitive or Float object back to String containing floating-point value :- String concatenation
- Float.toString() method
- String.valueOf() method
String concatenation with empty String method works for both float primitive and Float object, but the key is to use empty String first and then put floating-point value letter because then only Java understand that + operator is there for String concatenation and not for adding two floating-point value. If you use Float object then its toString() method is called automatically.
You can also see Core Java Volume 1 - Fundamentals by Cay S. Horstmann to learn more about different static factory methods to convert one type of variable to another primitive or wrapper type in Java.
BTW, if you already have Float object then you don't need to do String concatenation because calling Float.toString() will anyway give you floating point String you are looking for. The last method String.valueOf() takes a floating-point value and converts it to String, which is returned by this method, so don't forget to store the result because String objects are immutable in Java.
import java.util.Arrays; import java.util.List; /** * Java Program to convert String to float and vice-versa. * * @author WINDOWS 8 */ public class Test { public static void main(String args[]){ // let's first learn how to convert String to float in Java String decimal = "100.25"; // first we can use parseFloat() method to convert String to Float float f = Float.parseFloat(decimal); System.out.println("float equvialent of String " + decimal + " is : " + f); // remember it's not necessary that String always has floating point // so this is also Ok f = Float.parseFloat("200"); System.out.println("String 200 in float is : " + f); // another way to convert String to float is by using // valueOf() method String pie = "3.14"; float number = Float.valueOf(pie); System.out.println(pie + " in float is : " + number); // third way to parse String to float is by using Float constructor // though this will return object of Float wrapper class // autoboxing will convert it to float primitive String multiplier ="1.5"; float constant = new Float(multiplier); System.out.println("String '1.5' is equal to float : " + constant); // Now let's do the reverse // Now we have a float primitive and we need to convert // it to String in Java // simplest way to do is String concatenation float quarter = .25f; String fromFloat = "" + quarter; System.out.println("float " + f + " is converted to String : " + fromFloat); // Second easy way to convert float variable to String // is by using toString() // method of Float wrapper class Float half = Float.valueOf(.5f); String str = half.toString(); System.out.println("Float " + half + " is converted to String : " + str); // Another way to parse a floating point number as String by using // java.lang.String valueOf(float f) method // this method return float equivalent of String passed in. float onethird = .75f; String floatingpoint = String.valueOf(onethird); System.out.println("float " + onethird + " is equal to String : " + floatingpoint); } } Output float equvialent of String 100.25 is : 100.25 String 200 in float is : 200.0 3.14 in float is : 3.14 String '1.5' is equal to float : 1.5 float 200.0 is converted to String : 0.25 Float 0.5 is converted to String : 0.5 float 0.75 is equal to String : 0.75
That's all on how to convert String to float in Java and vice-versa. These kinds of little tricks help to improve productivity because you don't need to look back to google to find how to do this job. As an experienced Java developer, you must know how to do some basic stuff without looking at Google.
If you like this tutorial and interested in learning about data type conversion in Java, check out these amazing tutorials :
- How to convert Map to List in Java (tutorial)
- How to convert Double to Long in Java? (tutorial)
- How to convert Array to String in Java? (tutorial)
- 5 Ways to convert Java 8 Stream to List? (solution)
- How to convert a binary number to decimal in Java? (tutorial)
- 5 examples of converting InputStream to String in Java? (tutorial)
- How to convert Character to String in Java? (tutorial)
- 3 examples of converting Array to ArrayList in Java (tutorial)
- How to convert ArrayList to Set in Java? (tutorial)
Now, one question for you? What is your favorite way to convert String to Float in Java? Float.parseFloat() method or valueOf() method?
The option to convert float to String using constructor new Float() is not a good idea. In fact these constructors are deprecated from Java 9.
ReplyDelete