Type cast to Integer to avoid fraction value

 int roundedNumber = (int) 1.89;
Answer from PEHLAJ on Stack Overflow
🌐
W3Schools
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!");
    }


}
🌐
Quora
quora.com › How-do-you-round-down-in-Java
How to round down in Java - Quora
Answer (1 of 4): If you just want to round down to the nearest integer, you can use the floor method: [code]Math.floor(8.7); [/code]will give you 8.0 (note that this is a double).
🌐
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...
🌐
Medium
medium.com › @AlexanderObregon › rounding-numbers-with-math-round-math-floor-and-math-ceil-in-java-d201bbeb85e2
Java’s Math Rounding Methods Explained | Medium
March 7, 2025 - The Math.floor() method rounds a number downward to the next whole number, meaning it moves toward negative infinity. This is a strict truncation method that removes any decimal portion without checking its value.
Find elsewhere
🌐
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.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Math › floor
Java Math floor() - Round Down Value | Vultr Docs
December 3, 2024 - The Math.floor() method in Java is a crucial tool when it comes to rounding down floating-point numbers to the nearest integer that is less than or equal to the specified number.
🌐
Educative
educative.io › answers › how-to-use-the-java-mathround-method
How to use the Java Math.round() method
Round up if the decimal part is 0.5 or greater, and round down if it's less than 0.5.
🌐
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.
🌐
Learning Glue
learningglue.com › home › courses › java essentials
Java Math: Rounding Numbers - Learning Glue
April 11, 2024 - If you want to round down to the nearest whole numbers you can use the floor() methods. as the name suggest, as floors are usually under you, this method will take any floating point number and round it to the nearest lower whole number.
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-math-round-method
Java Math round() Method - GeeksforGeeks
May 13, 2025 - In this article, we are going to discuss how this method works for regular values and for special cases. Note: If the fraction is less than 0.5 then the number is rounded down and if the fraction is more than 0.5 then the number is rounded up.
🌐
How to do in Java
howtodoinjava.com › home › java math.ceil() vs. math.floor() vs. math.round()
Java Math.ceil() vs. Math.floor() vs. Math.round()
September 6, 2023 - In the following example, Math.floor(4.9) rounds down 4.9 to 4.0, ensuring the result is not greater than the original number.
🌐
Xenovation
xenovation.com › home › blog › java › rounding number in java
Rounding Number in Java | XENOVATION
April 6, 2019 - Java comes with two methods Math.ceil and Math.floor which round the number up and down respectively.
🌐
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.
🌐
SpigotMC
spigotmc.org › threads › how-to-round-down-a-double-in-java.57467
How to round down a double in java? | SpigotMC - High Performance Minecraft Software
March 28, 2015 - So, i've got this code that gets a random number between 2 numbers but what it generates is way too long double KillAmount2 =...
🌐
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)