Posts Tagged Math

Java – Math round() Example, Math round() Function, Remove decimal

Sunday, November 15th, 2009

Math class is part of java.lang package. Math round method returns the argument closest to integer value of the number. If fraction value is more than point 5, it will add 1 to original value nearest to positive value. round function return type is double or float. round function remove decimal number from value and return only number without decimal.

Math round example explains how to use Math round function in java.

public class JavaMathRoundExample {

    public static void main(String[] args) {

        double E1 = Math.round(2323.4393086905318781);

        double E2 = Math.round(2323.8393086905318781);

        double E3 = Math.round(-2323.8393086905318781);

        float F1 = Math.round(2323.43930);

        float F2 = Math.round(2323.8393);

        float F3 = Math.round(-2323.8393);

        System.out.println("Math round value :"+E1);
        System.out.println("Math round value :"+E2);
        System.out.println("Math round value :"+E3);

        System.out.println("Math round value :"+F1);
        System.out.println("Math round value :"+F2);
        System.out.println("Math round value :"+F3);
    }
}

Output

Math round value :2323.0
Math round value :2324.0
Math round value :-2324.0
Math round value :2323.0
Math round value :2324.0
Math round value :-2324.0