Home > DeveloperSection > Forums > Rounding a double in java
Garry Winogrand
Garry Winogrand

Total Post:24

Points:168
Posted on    October-05-2013 9:10 AM

 Java Java 
Ratings:


 1 Reply(s)
 723  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(System.in); //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");
     }
}


Dag Hammarskjold
Dag Hammarskjold

Total Post:24

Points:168
Posted on    October-05-2013 9:29 AM

You can print to two decimal places using the following.

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

Don't want to miss updates? Please click the below button!

Follow MindStick