Type cast to Integer to avoid fraction value
int roundedNumber = (int) 1.89;
Answer from PEHLAJ on Stack OverflowW3Schools
w3schools.com › java › ref_math_floor.asp
Java Math floor() 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 down to the nearest integer: System.out.println(Math.floor(0.60)); System.out.println(Math.floor(0.40)); System.out.println(Math.floor(5)); System.out.println(Math.floor(5.1)); System.out.println(Math.floor(-5.1)); System.out.println(Math.floor(-5.9)); Try it Yourself » ·
Top answer 1 of 4
5
Type cast to Integer to avoid fraction value
int roundedNumber = (int) 1.89;
2 of 4
1
This question is not very clear, there are an unlimited number of possible solutions, here are two of them:
public class WeirdQuestion {
// Assuming 0<=threshold<=1
public static double ThresholdRoundMethodOne(double value, double threshold) {
return Math.round(value-(1.0-threshold));
}
// No limitations for threshold
public static double ThresholdRoundMethodTwo(double value, double threshold) {
threshold = threshold-(int)threshold;
return Math.round(value-(1.0-threshold));
}
public static void main(String[] args) {
double threshold = 0.8;
System.out.println("MethodOne: 1.08: "+ThresholdRoundMethodOne(1.08, threshold));
System.out.println("MethodOne: 1.56: "+ThresholdRoundMethodOne(1.56, threshold));
System.out.println("MethodOne: 1.67: "+ThresholdRoundMethodOne(1.67, threshold));
System.out.println("MethodOne: 1.98: "+ThresholdRoundMethodOne(1.98, threshold));
System.out.println("MethodOne: 1.89: "+ThresholdRoundMethodOne(1.89, threshold));
System.out.println("MethodOne: 1.8 : "+ThresholdRoundMethodOne(1.8, threshold));
threshold = 1.8;
System.out.println("------------------------------------------------------------");
System.out.println("MethodTwo: 1.08: "+ThresholdRoundMethodTwo(1.08, threshold));
System.out.println("MethodTwo: 1.56: "+ThresholdRoundMethodTwo(1.56, threshold));
System.out.println("MethodTwo: 1.67: "+ThresholdRoundMethodTwo(1.67, threshold));
System.out.println("MethodTwo: 1.98: "+ThresholdRoundMethodTwo(1.98, threshold));
System.out.println("MethodTwo: 1.89: "+ThresholdRoundMethodTwo(1.89, threshold));
System.out.println("MethodTwo: 1.8 : "+ThresholdRoundMethodTwo(1.8, threshold));
System.out.println("Done!");
}
}
Videos
02:16
Java Math round() Method - YouTube
02:54
Java Tutorial Rounding Numbers - YouTube
11:28
Show Me HOW TO ROUND NUMBERS IN JAVA - YouTube
05:32
Java Tutorial - 14 - Rounding Numbers, Ceiling, and Floor (Math ...
03:00
java round down double to int - YouTube
07:21
Math Class Part 2: Rounding Methods (Java) - YouTube
Reddit
reddit.com › r/learnjava › do i round up or down in java?
r/learnjava on Reddit: Do I round up or down in java?
May 12, 2023 -
I'm just starting a java course (online) and I'm already confused with the math. When dividing ints and if I get a decimal, do I round down no matter what? I tried googling the answer and saw some things I didn't understand and some conflicting answers. For example: Int 5/3 = 1.66, but would the answer be 1 here?
Top answer 1 of 9
13
You don't always do one thing. You can decide how you want to round: https://www.baeldung.com/java-round-decimal-number You just do what is right for your application.
2 of 9
11
It doesn't "round down", the division operator cuts off when dividing integers. With the modulo operator you'll get the remainder. If you want a decimal you should use double (and use at least one double in the operation, otherwise the operation will be executed as an integer operation and then casted to a double, i.e. double x = 5/3;, x will be 1.0, double x = 5.0/3; x will be 1.6667)
Scaler
scaler.com › home › topics › round off in java
Java Math.round() - Scaler Topics
April 19, 2024 - It takes a single argument of type double or float. It returns the rounded value as a long or an int, respectively. If the fractional part of the number is less than 0.5, it rounds down...
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Math.html
Math (Java Platform SE 8 )
1 week ago - When discussing the accuracy of a method as a whole rather than at a specific argument, the number of ulps cited is for the worst-case error at any argument. If a method always has an error less than 0.5 ulps, the method always returns the floating-point number nearest the exact result; such a method is correctly rounded.
Programiz
programiz.com › java-programming › library › math › floor
Java Math floor()
Become a certified Java programmer. Try Programiz PRO! ... The floor() method rounds the specified double value downward and returns it. The rounded value will be equal to a mathematical integer.
TestMu AI Community
community.testmu.ai › ask a question
Is Math.round() always reliable for double-to-int conversion in Java? - TestMu AI Community
January 16, 2025 - How do I convert a double to an int in Java without unintended truncation? In Java, I know that casting a double to an int truncates the decimal part: double x = 1.5; int y = (int) x; // y = 1 Using Math.round(x) seems to solve this by rounding correctly: int y = (int) Math.round(x); // y = 2 However, since floating-point numbers sometimes have imprecise representations (e.g., 1.9999999998), could there be cases where Math.round(x) still results in an unexpected truncation instead of ...
Naukri
naukri.com › code360 › library › mathround-in-java
Java Math.round() Method Explained with Examples
September 18, 2025 - Almost there... just a few more seconds
Coderanch
coderanch.com › t › 370870 › java › Rounding-Numbers-Truncating
Rounding Numbers Down / Truncating (Java in General forum at Coderanch)
April 1, 2003 - Java performs proper rounding when converting to integer, so this would work: ((int)(amount * 100 - 50))/100.0 If, on the other hand, you prefer something more readable, Math.floor(amount * 100) / 100 Would work just as well - Peter · Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd ... That won't round down...
Learnjavacoding
learnjavacoding.com › rounding-java
Rounding – Learn Java Coding
You do the input times 2, in this case, 11.2. Then plus 0.5, that is 11.7. Then round you round the number down, to 11, like we did in 3. And then divide by 2.0 because you want to make it double again, so use 2.0 instead of 2.
Coderanch
coderanch.com › t › 497838 › java › int
Round up int [Solved] (Beginning Java forum at Coderanch)
June 2, 2010 - Other options... int x = Math.round((729.0/10.0)); OR int x = (int)Math.ceil((729.0/10.0)); Henry · Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)