Advertise with us

Rounding a double in java

Posted by  Garry Winogrand
 1548  View(s)
Rate this:

I got project do convert from cm to inch. I did it: how can i round my number with Math.round? 

public class Centimer_Inch

public static void main (String[] args)
        // 2.54cm is 1 inch
       Scanner cm = new Scanner(; //Get INPUT from pc-Keyboard
       System.out.println("Enter the CM:"); // Write input
       double centimeters = cm.nextDouble();
       double inches = centimeters/2.54;
       System.out.println(inches + " Inch Is " + centimeters + " centimeters");
  1. Re: Rounding a double in java

    You can print to two decimal places using the following.

     System.out.printf("%.2f inch is %.2f centimeters%n", inches, centimeters);

      Modified On Mar-31-2018 06:19:23 AM