Languages
[Edit]
EN

Java - Math.round() method example

0 points
Created by:
TylerH
448

Math.round is a static method that returns a number that is rounded to the closest integer number.

public class MathExample {

    public static void main(String[] args) {
        System.out.println( Math.round(  5     ) ); //  5

        System.out.println( Math.round(  2.49  ) ); //  2
        System.out.println( Math.round(  2.50  ) ); //  3
        System.out.println( Math.round(  2.51  ) ); //  3

        System.out.println( Math.round( -2.49  ) ); // -2
        System.out.println( Math.round( -2.50  ) ); // -2
        System.out.println( Math.round( -2.51  ) ); // -3

        System.out.println( Math.round(  0.999 ) ); //  1
        System.out.println( Math.round(  1.001 ) ); //  1
        System.out.println( Math.round( -1.001 ) ); // -1
    }
}

1. Documentation

Syntax
package java.lang;

public final class Math {

    public static long round(double number) { ... }
    public static int round(float number) { ... }

}

Note: Classes in the java.lang package are imported automatically, so it is not necessary to do it manually - we use just Math.round() call.

Parametersnumber - primitive value.
ResultRounded number value (primitive value).
Descriptionround is a static method that returns a number that is rounded to the closest integer number.

 

2. Custom round method examples

2.1. Rounding with precision to n places example

This section contains a custom function that shows how to round number with precision to n places.

public class MathExample {

    static double roundPrecised(double number, int precision) {
        double power = Math.pow(10, precision);
        return Math.round(number * power) / power;
    }

    public static void main(String[] args) {
        // Example:

        System.out.println( roundPrecised(     5  ,  0 ) ); // 5.0
        System.out.println( roundPrecised(     5. ,  0 ) ); // 5.0
        System.out.println( roundPrecised(      .5,  0 ) ); // 1.0

        System.out.println( roundPrecised(  1.2345,  0 ) ); // 1.0
        System.out.println( roundPrecised(  1.2345,  1 ) ); // 1.2
        System.out.println( roundPrecised(  1.2345,  2 ) ); // 1.23
        System.out.println( roundPrecised(  1.2345,  3 ) ); // 1.235

        System.out.println( roundPrecised( -1.2345,  0 ) ); // -1.0
        System.out.println( roundPrecised( -1.2345,  1 ) ); // -1.2
        System.out.println( roundPrecised( -1.2345,  2 ) ); // -1.23
        System.out.println( roundPrecised( -1.2345,  3 ) ); // -1.234

        System.out.println( roundPrecised(   12345, -1 ) ); // 12350.0
        System.out.println( roundPrecised(   12345, -2 ) ); // 12300.0
        System.out.println( roundPrecised(   12345, -3 ) ); // 12000.0
    }
}

2.2. Round implementation example

This section contains custom round function implementation.

public class MathExample {

    static double roundNumber(double value) {
        if (value < 0.0) {
            double rest = (value % 1.0);
            if (rest < -0.5) {
                rest += 1.0;
            }
            return value - rest;
        } else {
            value += 0.5;
            return value - (value % 1.0);
        }
    }

    public static void main(String[] args) {
        // Example:

        System.out.println( roundNumber(  5     ) ); //  5.0

        System.out.println( roundNumber(  2.49  ) ); //  2.0
        System.out.println( roundNumber(  2.50  ) ); //  3.0
        System.out.println( roundNumber(  2.51  ) ); //  3.0

        System.out.println( roundNumber(  0.999 ) ); //  1.0
        System.out.println( roundNumber(  1.001 ) ); //  1.0

        System.out.println( roundNumber( -2.49  ) ); // -2.0
        System.out.println( roundNumber( -2.50  ) ); // -2.0
        System.out.println( roundNumber( -2.51  ) ); // -3.0

        System.out.println( roundNumber( -1.001 ) ); // -1.0
    }
}

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Cross technology - Math.round()

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join