Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

How to use rounding math methods in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        System.out.println("1) " + Math.ceil(3.1));
        System.out.println("2) " + Math.ceil(3.0));
        System.out.println("3) " + Math.ceil(3.5));
        System.out.println("4) " + Math.ceil(3.6));
        System.out.println("5) " + Math.ceil(-3.0));
        System.out.println("6) " + Math.ceil(-3.1));
        System.out.println("7) " + Math.ceil(-3.5));
        System.out.println("8) " + Math.ceil(-3.6));
        
        System.out.println("9) " + Math.floor(3.1));
        System.out.println("10) " + Math.floor(3.0));
        System.out.println("11) " + Math.floor(3.5));
        System.out.println("12) " + Math.floor(3.6));
        System.out.println("13) " + Math.floor(-3.0));
        System.out.println("14) " + Math.floor(-3.1));        
        System.out.println("15) " + Math.floor(-3.5));        
        System.out.println("16) " + Math.floor(-3.6));
        
        System.out.println("17) " + Math.rint(3.0));
        System.out.println("18) " + Math.rint(3.1));
        System.out.println("19) " + Math.rint(3.5));
        System.out.println("20) " + Math.rint(3.6));
        System.out.println("21) " + Math.rint(-3.0));
        System.out.println("22) " + Math.rint(-3.1));
        System.out.println("23) " + Math.rint(-3.5));
        System.out.println("24) " + Math.rint(-3.6));
        
        System.out.println("25) " + Math.round(3.1));        
        System.out.println("26) " + Math.round(3.6f));
        System.out.println("27) " + Math.round(3.5));
        System.out.println("28) " + Math.round(3.6));
        System.out.println("29) " + Math.round(4.0));
        System.out.println("30) " + Math.round(-4.0f));
        System.out.println("31) " + Math.round(-3.1));
        System.out.println("32) " + Math.round(-3.5));
        System.out.println("33) " + Math.round(-3.6));
    }
}



/*
run:

1) 4.0
2) 3.0
3) 4.0
4) 4.0
5) -3.0
6) -3.0
7) -3.0
8) -3.0
9) 3.0
10) 3.0
11) 3.0
12) 3.0
13) -3.0
14) -4.0
15) -4.0
16) -4.0
17) 3.0
18) 3.0
19) 4.0
20) 4.0
21) -3.0
22) -3.0
23) -4.0
24) -4.0
25) 3
26) 4
27) 4
28) 4
29) 4
30) -4
31) -3
32) -3
33) -4

*/

 



answered Jun 11, 2019 by avibootz

Related questions

1 answer 191 views
191 views asked Jun 10, 2019 by avibootz
1 answer 156 views
1 answer 161 views
1 answer 109 views
109 views asked Sep 21, 2023 by avibootz
...