Yesterday one of the new joiner Java Trainee Engineers from our team came to me and asked about how do you convert a float variable into a long or int data type? He was storing some values coming from another system in the database and only wanted to store values before the decimal point e.g. he was getting "3.144" and he wants to convert it to "3" to store into the database. The good thing was that API was returning a float primitive value and you don't need to convert a String to float etc. I asked him whether he needs routing or not, which he wasn't sure but it turns out that he didn't need that. I explained to him how to do that and that's what you will find in this article as well. In short, there are 3 ways to convert a float value into long or int in Java, but we will only focus on the long data type part.
The first way to convert a float data type into a long value is to auto-box float primitive into Float object and calls the longValue() method. This is a more structured way as other ways are simply to cast a float to long or int to get rid of decimal points.
You can also write the Java program by following these tips to convert a float value to an int by replacing the long method with their int counterpart.
Alternatively, you can use Math.round() and then cast back to long data type.
There is no doubt, the second approach is the easiest if the requirement is simply getting rid of anything after a decimal point but if you need rounding then the third approach is the right way to convert a float data type to long in Java.
In this tutorial, you will learn all three ways to perform float to long conversion in Java. Btw, If you have just started learning Java or want to fill gaps in your knowledge about Java programming then I suggest you join a comprehensive course like The Complete Java Masterclass on Udemy. It's not costly, you can get it at the cost of a coffee and learn Java better.
It will not do anything special but just discard anything after the decimal point so you will have value 3 in the fromFloat variable.
If you want to convert float to int then instead of casting to long you should cast float into an int. This is the easiest way to do this conversion. If you want to learn more about type casting in Java then I also suggest you take a look at Java Fundamentals: The Java Language course on Pluralsight.
The first way to convert a float data type into a long value is to auto-box float primitive into Float object and calls the longValue() method. This is a more structured way as other ways are simply to cast a float to long or int to get rid of decimal points.
You can also write the Java program by following these tips to convert a float value to an int by replacing the long method with their int counterpart.
Alternatively, you can use Math.round() and then cast back to long data type.
There is no doubt, the second approach is the easiest if the requirement is simply getting rid of anything after a decimal point but if you need rounding then the third approach is the right way to convert a float data type to long in Java.
In this tutorial, you will learn all three ways to perform float to long conversion in Java. Btw, If you have just started learning Java or want to fill gaps in your knowledge about Java programming then I suggest you join a comprehensive course like The Complete Java Masterclass on Udemy. It's not costly, you can get it at the cost of a coffee and learn Java better.
3 ways to convert a float to long in Java
Let's see different approaches to converting a float value to long or int in Java and understand the pros and cons of each approach. Java supports type casting and that should be the standard way for data type conversion.Solution 1 - Casting
As you know that Java supports type casting and it's also the standard way to convert one data type into another, but you can only typecast higher values into lower. Since float has a higher range than long, you can cast it to long using typecasting, as shown below :float number = 444.33f; long aValue = (long) number; // 444
It will not do anything special but just discard anything after the decimal point so you will have value 3 in the fromFloat variable.
If you want to convert float to int then instead of casting to long you should cast float into an int. This is the easiest way to do this conversion. If you want to learn more about type casting in Java then I also suggest you take a look at Java Fundamentals: The Java Language course on Pluralsight.
Solution 2 - Float.longValue
Another way to convert a float to long is by first auto-boxing float value into Float wrapper object and then calling Float.longValue() method. This method internally cast float value into long as shown in our first example and below code :public long longValue() { return (long)value; }
and here is how you can use this method for conversion :
Float PIE = 3.14f; long fromFloat = PIE.longValue();
This method is more suitable when you have a Float object rather than a float primitive value.
Solution 3 - Math.round and Casting
Sometimes the conversion is not that straightforward as discarding anything after the decimal point. You might need to do the rounding first and conversion later. If that's the case then you can use the Math.round() method for rounding first and typecasting later to convert double to long, because Math.round() method returns a double value.Here is sample code to do this conversion :
You can see the effect of rounding, in the first example float was rounded down and that's why a converted long value is 333 while in the second example it was rounded up and that's why the long value is 334.
If you want to learn more about rounding and type casting in Java, I also suggest you take a look at Core Java for the Impatient book by Cay S. Horstmann, one of the better books to learn Java, which covers concepts like this in good detail.
That's all about how to convert float to a long data type in Java. Typecasting is the simplest and right way to do it if your goal is just to get rid of decimal values. If you want to round before getting rid of decimal points then you can use Math.round() method. BTW, that's not the only way to round numbers in Java, but it works for most purposes.
Here is also a nice summary of all three approaches :
Btw, If you are new to Java and puzzled about who to convert one thing to another like String to integer, or String to Date then you should check out some of the data type conversion tutorials from this blog :
// Using Math.round() and cast back to long float points = 333.322f; long rounded = Math.round(points); System.out.printf("float : %f, long : %d %n", points, rounded); points = 333.922f; rounded = Math.round(points); System.out.printf("float : %f, long : %d %n", points, rounded); Output : float : 333.321991, long : 333 float : 333.921997, long : 334
You can see the effect of rounding, in the first example float was rounded down and that's why a converted long value is 333 while in the second example it was rounded up and that's why the long value is 334.
If you want to learn more about rounding and type casting in Java, I also suggest you take a look at Core Java for the Impatient book by Cay S. Horstmann, one of the better books to learn Java, which covers concepts like this in good detail.
Java Program to convert float to a long data type
Here is the Java program which combines all three ways to convert a float to long in Java. You can see how they work and learn the technique to perform another similar conversion like float to int, short, or byte or double to long, int, short, and byte./** * Java Program to convert float to long in Java * * @author Javarevisited */ public class FloatToLongConverter{ public static void main(String[] args) { // Autobox float into Float and then call Float.longValue(); Float PIE = 3.14f; long fromFloat = PIE.longValue(); System.out.printf("float value %f, long value %d %n", PIE, fromFloat); // Simple cast to get rid of decimals float number = 444.33f; long aValue = (long) number; System.out.printf("float value %f, after casting into long %d %n", number, aValue); // Using Math.round() and cast back to long float points = 333.322f; long rounded = Math.round(points); System.out.printf("float : %f, long : %d %n", points, rounded); } } Output : float value 3.140000, long value 3 float value 444.329987, after casting into long 444 float : 333.321991, long : 333
That's all about how to convert float to a long data type in Java. Typecasting is the simplest and right way to do it if your goal is just to get rid of decimal values. If you want to round before getting rid of decimal points then you can use Math.round() method. BTW, that's not the only way to round numbers in Java, but it works for most purposes.
Here is also a nice summary of all three approaches :
Btw, If you are new to Java and puzzled about who to convert one thing to another like String to integer, or String to Date then you should check out some of the data type conversion tutorials from this blog :
Other Java Programming articles you may like
- How do you convert String to ASCII value in Java? (answer)
- How to convert a list to Stream in Java 8? (example)
- How do you convert Java Object to XML using JAXB? (example)
- How to convert Fahrenheit to Celsius in Java? (program)
- String to Date conversion in Java using SimpleDateFormat class (solution)
- 5 ways to convert InputStream to String in Java? (solution)
- How do you format Date to String in Java? (answer)
- XMLGregorianCalendar to Date in Java and Vice-versa (solution)
- How do you convert Double to String in Java? (solution)
- How do you convert Binary to Decimal in Java? (answer)
No comments :
Post a Comment