How to Use Math.rint() in Java


In this example, we will use Math.rint() to get the closest integer value.

Source Code

package com.beginner.examples;

public class RintExample {

    public static void main(String[] args){
    	//return the closest integer value.
    	double a = -10.8;
    	double b = 0;
    	double c = 2.01;
    	System.out.println("rint of " + a + " is: " + Math.rint(a));
        System.out.println("rint of " + b + " is: " + Math.rint(b));
        System.out.println("rint of " + c + " is: " + Math.rint(c));
    }
}

Output:

rint of -10.8 is: -11.0
rint of 0.0 is: 0.0
rint of 2.01 is: 2.0
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments