How to round a number up to the nearest 10 in Java

1 Answer

0 votes
public class MyClass {
     static int roundUpToNearest10(double num) {
        return (int)Math.ceil(num / 10) * 10;
    }
    public static void main(String args[]) {
        System.out.println(roundUpToNearest10(33));
        System.out.println(roundUpToNearest10(59));
        System.out.println(roundUpToNearest10(599.99));
        System.out.println(roundUpToNearest10(3.14));
        System.out.println(roundUpToNearest10(2));
        System.out.println(roundUpToNearest10(19));
        System.out.println(roundUpToNearest10(-12));
        System.out.println(roundUpToNearest10(-101));
        System.out.println(roundUpToNearest10(-109));
    }
}



/*
run:
   
40
60
600
10
10
20
-10
-100
-100
   
*/

 



answered Jun 10, 2022 by avibootz

Related questions

1 answer 123 views
1 answer 124 views
1 answer 112 views
1 answer 129 views
1 answer 110 views
1 answer 97 views
1 answer 115 views
...