# How to convert float to long or int in Java?

Yesterday one of the junior from my team came to me asking about how do you I convert a float variable into long or int? He was storing some values coming from another system in the database and only wanted to store value before the decimal point e.g. he was getting "3.144" and he want to convert it to "3" to store into database. Good thing was that API was returning a float primitive value and you don't need to convert a String to float etc. There are 3 ways to convert a float value into long or int in Java, but we will only focus on long part. You can write the Java program by following tips to convert float to an int by replacing long method to their int counterpart. The first way is to auto-box float primitive into Float object and call the longVale() method and another way is simply cast a float to long or int to get rid of decimal points.

Alternative you can use Math.round() and then cast back to long. No doubt, the second approach is the easiest if the requirement is simply getting rid of anything after decimal point but if you need to rounding then the third approach is the right way to go.

In this tutorial, we will learn all three ways to perform float to long conversion in Java. Btw, if you have just started learning Java then you can also read Core Java for the Impatient By Cay S. Horstmann, one of the better books to learn Java, which covers concept like this in good detail.

## 3 ways to convert float to long in Java

Let's see different approaches to converting a float value to long or int in Java and understand 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 support 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 higher range than long, you can cast it to long using type casting, 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.

### 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 float primitive value.

### Solution 3 - Math.round and Casting

Sometimes 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 Math.round() method for rounding first and type casting later to convert  double to long, because Math.round() method returns a double value. Here is sample code to do this conversion :

```// 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 first example float was rounded-down and that's why converted long value is 333 while in the second example it was rounded up and that's why the long value is 334.

Here is nice summary of all three approaches :

## Java Program to convert float to long value

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 e.g. 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. Type casting is the simplest and right way to do 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.

If you are new to Java and puzzled on who to convert one thing to another e.g. String to integer, or String to date then you should check out some of the data type conversion tutorials from this blog :

Further Learning
Java Fundamentals: The Java Language by Jim Wilson
What's New in Java 8
What's New in Java 9 By Sander Mak

• 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)

Hope this helps.