how to round to int in java

www.la‮uttu‬ri.com
how to round to int in java
/**
 * @author lautturi.com
 * Java example: round number in java
 */

import java.util.*;

public class Lautturi {

	public static void main(String[] args) {

		double x = 3.14;

		//Rounds to nearest int
		Math.round(x);
		
		//Rounds up to int
		Math.ceil(x);
		
		//Rounds down to int
		Math.floor(x);		
		
		System.out.println(x+": "+Math.round(x));
		System.out.println(x+": "+Math.ceil(x));
		System.out.println(x+": "+Math.floor(x));
        
		System.out.println("round -1.6: "+Math.round(-1.6));
		System.out.println("round -1.45: "+Math.round(-1.45));
		System.out.println("round -0.99: "+Math.round(-0.99));
		System.out.println("round 1.6: "+Math.round(1.6));
		System.out.println("round 1.45: "+Math.round(1.45));
		System.out.println("round 0.99: "+Math.round(0.99));
        
		System.out.println("ceil -1.6: "+Math.ceil(-1.6));
		System.out.println("ceil -1.45: "+Math.ceil(-1.45));
		System.out.println("ceil -0.99: "+Math.ceil(-0.99));
		System.out.println("ceil 1.6: "+Math.ceil(1.6));
		System.out.println("ceil 1.45: "+Math.ceil(1.45));
		System.out.println("ceil 0.99: "+Math.ceil(0.99));
        
		System.out.println("floor -1.6: "+Math.floor(-1.6));
		System.out.println("floor -1.45: "+Math.floor(-1.45));
		System.out.println("floor -0.99: "+Math.floor(-0.99));
		System.out.println("floor 1.6: "+Math.floor(1.6));
		System.out.println("floor 1.45: "+Math.floor(1.45));
		System.out.println("floor 0.99: "+Math.floor(0.99));

	}
}

output:

3.14: 3
3.14: 4.0
3.14: 3.0
round -1.6: -2
round -1.45: -1
round -0.99: -1
round 1.6: 2
round 1.45: 1
round 0.99: 1
ceil -1.6: -1.0
ceil -1.45: -1.0
ceil -0.99: -0.0
ceil 1.6: 2.0
ceil 1.45: 2.0
ceil 0.99: 1.0
floor -1.6: -2.0
floor -1.45: -2.0
floor -0.99: -1.0
floor 1.6: 1.0
floor 1.45: 1.0
floor 0.99: 0.0
Created Time:2017-09-14 23:06:30  Author:lautturi