search
HomeJavajavaTutorialHow to convert float to long or int in Java? (with code)

The content of this article is about how to convert float to long or int in Java? (Attached is the code), which has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

In Java, there are three ways to convert float to long or int, but we only focus on the long data type part.

The first way to convert a float data type to a long value is to convert the auto-box float primitive to a float object and call the longValue() method.

This is a more structured approach as the other methods simply convert the float to long or int to remove the decimal point.

You can also write a Java program according to the following tips to convert float to int by replacing the long method with the corresponding int method.

Alternatively, you can use Math.round() and then convert it back to the long data type.

The second method is the simplest if you just remove the digits after the decimal point, but if you need to round, then the third method is the best to convert the float data type to long.

In this article, you will learn three ways to perform float to long conversion in Java.

3 ways to convert float to long in Java

Option 1-Casting

Java supports type conversion, which is to convert a A data type is converted to another data type, but only a higher value can be converted to a lower value. Since float has a longer range than long, you can use a type cast to convert it to long like this:

float number = 444.33f; 
long aValue = (long) number; // 444

It won't do anything but discard the value after the decimal point, so it will be in the fromFloat variable Get a value of 3.

If you want to convert float to int, then you should convert float to integer instead of long. This is the simplest conversion method.

Solution 2 - Float.longValue

Another way to convert a float to a long is to first autoload the float value into a Float wrapper object and then call the Float.longValue() method. This method internally converts a floating point value to a long, as shown in the above image and the code below:

public long longValue(){
 return(long)value;
}

Here is how to convert using this method:

Float PIE = 3.14f; 
long fromFloat = PIE.longValue();

When you have a Float object and This method is more appropriate when it is not a float primitive value.

Solution 3 - Math.round and Casting

Sometimes conversion isn't as simple as discarding everything after the decimal point. Rounding may be required before conversion. If this is the case, you can use the Math.round() method to perform rounding first, and then perform type conversion to convert the double to long. The Math.round() method returns a double value. Here is the sample code to achieve this conversion:

// 使用Math.round()并强制转换为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, the float is rounded down, which is why the converted long value is 333, And in the second example it is rounded, that's why the long value is 334.

Java program to convert float to long data type

The code below As shown, it combines three methods to convert float to long. You can see how they work and learn how to perform another similar conversion technique such as float to int, short or byte or double to long, int, short and byte.

/** 
* 用于在Java中将float转换为long
* 
* @author Javarevisited 
*/ 
public class FloatToLongConverter{ 

 public static void main(String[] args) { 

 // Autobox浮动到Float然后调用Float.longValue();
 Float PIE = 3.14f; 
 long fromFloat = PIE.longValue(); 

 System.out.printf("float value %f, long value %d %n", PIE, fromFloat); 
 
 // 简单的类型转换来消除小数 
 float number = 444.33f; 
 long aValue = (long) number; 
 System.out.printf("float value %f, after casting into long %d %n", number, aValue); 
 
 // 使用Math.round()并将其转换回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

This is how to convert float to long data type in Java. If you are just removing decimal values, type conversion is the easiest way. This is not the only way to round numbers in Java, but it works for most purposes.

If you want to learn faster and more systematically, come and join the group chat, which includes Alibaba, Baidu interview questions, Java engineering, high performance and distribution, microservices, performance tuning Spring, MyBatis , explanation of Netty source code analysis and other knowledge points [Java Architect Resource Sharing Group] (group number 142019080), directly click the link to join the group. https://jq.qq.com/?_wv=1027&k=5lXBNZ7

The above is the detailed content of How to convert float to long or int in Java? (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools