How to round double and float number in Java

1 Answer

0 votes
package javaapplication1;

public class JavaApplication1 {
 
    public static void main(String[] args) {
        
        double d1 = 30.678;
        double d2 = 30.500;
        double d3 = 30.510;
        double d4 = 30.400;
        double d5 = 30.410;
        float f1 = 22;
        float f2 = 3.14f;
        float f3 = 3.49f;
        float f4 = 3.50f;
        float f5 = 3.51f;

        System.out.println(Math.round(d1)); // long round(double d)
        System.out.println(Math.round(d2));
        System.out.println(Math.round(d3));
        System.out.println(Math.round(d4));
        System.out.println(Math.round(d5));
        System.out.println(Math.round(f1)); // int round(float f)
        System.out.println(Math.round(f2));
        System.out.println(Math.round(f3));
        System.out.println(Math.round(f4));
        System.out.println(Math.round(f5));
    }
}
 
/*
run:

31
31
31
30
30
22
3
3
4
4
 
*/

 



answered Sep 8, 2016 by avibootz

Related questions

...