For your example, it seems that you want to use Math.rint(). It will return the closest integer value given a double.

int valueX = (int) Math.rint(x);
int valueY = (int) Math.rint(y);   
Answer from Makoto on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_math_round.asp
Java Math round() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate · ❮ Math Methods · Round numbers to the nearest integer: System.out.println(Math.round(0.60)); System.out.println(Math.round(0.40)); System.out.println(Math.round(5)); System.out.println(Math.round(5.1)); System.out.println(Math.round(-5.1)); System.out.println(Math.round(-5.9)); Try it Yourself » ·
🌐
Educative
educative.io › answers › how-to-use-the-java-mathround-method
How to use the Java Math.round() method
The Math.round() method rounds a number to the nearest integer by adding 0.5 and flooring the result.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › rint
Java Math rint() - Round To Nearest Integer | Vultr Docs
December 3, 2024 - The rint() method in Java is a built-in function in the Math class that is used to round a double value to the nearest integer using conventional rounding rules. This function adheres to half-up rounding, where values that are exactly midway ...
🌐
Programiz
programiz.com › java-programming › library › math › round
Java Math round()
Created with over a decade of experience. ... Created with over a decade of experience and thousands of feedback. ... Try Programiz PRO! ... Become a certified Java programmer. Try Programiz PRO! ... The round() method rounds the specified value to the closest int or long value and returns it.
🌐
Quora
quora.com › How-do-I-round-a-decimal-to-nearest-integer-in-Java
How to round a decimal to nearest integer in Java - Quora
Answer (1 of 10): Use the [code ]round()[/code], [code ]floor()[/code] and [code ]ceil()[/code] static methods in the Math class. [code ]round()[/code] will round to the nearest integer, whilst [code ]floor()[/code] will round down and [code ...
🌐
Sololearn
sololearn.com › en › Discuss › 2886933 › in-java-how-do-we-make-a-type-double-round-up-to-the-nearest-integer
In Java, how do we make a type double round UP to the nearest integer? | Sololearn: Learn to code for FREE!
Martin Taylor casting a float number to integer number will round up for negative numbers and round down for positive numbers. Edit: it seems you have edited your answer ... double a=12345.2222; int s1=(int)Math.ceil(a); int s2=(int)Math.floor(a); int s3=(int)Math.round(a); //s1=12346 //s2 & s3 =12345
🌐
Reddit
reddit.com › r/javahelp › how do i round an int to the nearest number?
r/javahelp on Reddit: How do I round an int to the nearest number?
October 2, 2017 -

Hello r/javahelp! I'm learning Java on my own and have been doing some exercises a friend gave me of his past classes when I expressed interest in coding. One of the exercises is to make a program where you convert yards into miles only using ints and a set of numbers given (Example: 60000/70000). Here's the current rendition of the code. For some reason I can't get the numbers that should round, to round (Example: 70000 should round to 40, but I still get 39). I've even tried Math.round() but it doesn't seem to work. In the code, I was hoping that adding 0.5 would bump the round up to 40, but it doesn't. It's really perplexing.

Find elsewhere
🌐
Vertex Academy
vertex-academy.com › tutorials › en › rounding-numbers-java
Rounding of Numbers in Java • Vertex Academy
December 7, 2017 - 3. Math.ceil() - this method rounds a number upward to its nearest integer. At first, we have 5.25, and then this method gives us 6.0.
🌐
Scaler
scaler.com › home › topics › round off in java
Java Math.round() - Scaler Topics
April 19, 2024 - ... Step 2: Take the floor of the value obtained after adding 1/2. ... b. Parameter The Math.round() method takes floating-point numbers, i.e., both float and double data types, as arguments. c. Returns This method returns the nearest integer ...
🌐
Alvin Alexander
alvinalexander.com › java › how-to-round-float-double-to-int-integer-in-java
Java: How to round a float or double to an integer | alvinalexander.com
Java math FAQ: How do I round a float or double number to an integer in Java? Solution: Use Math.round() to round a float (or double) to the nearest integer (int) in Java.
🌐
Quora
quora.com › How-do-I-round-of-integer-to-its-nearest-integer-value-in-Java
How to round of integer to its nearest integer value in Java - Quora
Answer (1 of 2): Although Math.round() ... to round off a number to nearest tens. (Let n=1674) It can be done by: (int)(Math.round( n / 10.0) * 10) n / 10.0 ......
🌐
GeeksforGeeks
geeksforgeeks.org › java-math-round-method-example
Java Math round() method with Example - GeeksforGeeks
April 11, 2018 - Return Type: This method returns the closest rounded value as long. Now, we are going to discuss some examples for better understanding and clarity. Example 1: In this example, we will see the basic usage of Math.round() method.
🌐
Codecademy
codecademy.com › docs › java › math methods › .round()
Java | Math Methods | .round() | Codecademy
October 24, 2022 - Beginner Friendly.Beginner Friendly17 hours17 hours ... The .round() method takes one parameter n, the value that is to be rounded. The following example demonstrates using .round() to round the value 1.8 to the nearest integer...
🌐
Coderanch
coderanch.com › t › 675673 › java › Rounding-double-nearest-integer
Rounding off a double value to the nearest integer [Solved] (Beginning Java forum at Coderanch)
You problem probably appears when you do the division, an integer division. 13 + 17 + 23 = 53 53 / 3 = 17 (int) 17 you get 17 You need to divide by 3.0 in order to get an answer in double, then round, then cast to int.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-math-round-method
Java Math round() Method - GeeksforGeeks
May 13, 2025 - Return Type: This method returns the closest rounded value as long. Now, we are going to discuss some examples for better understanding and clarity. Example 1: In this example, we will see the basic usage of Math.round() method.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › round
Java Math round() - Round Decimal Value | Vultr Docs
December 3, 2024 - This code snippet rounds the float value 123.456 to 123 because Math.round() rounds to the nearest integer.
🌐
W3Schools
w3schools.com › java › ref_math_floor.asp
Java Math floor() Method
Tip: To round a number UP to the nearest integer, look at the ceil() method.
🌐
Xenovation
xenovation.com › home › blog › java › rounding number in java
Rounding Number in Java | XENOVATION
April 6, 2019 - At times, we want specific behavior for rounding up or rounding down. Ceil and Floor are functions in Java which give you nearest integer down or up. ... The java math library provides a static ceil function which accepts a double.