ROUNDING A DOUBLE IN JAVA

Garry Winogrand

Total Post:24

Points:168
Posted by  Garry Winogrand
Java 
 975  View(s)
Ratings:
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");
     }
}
  1. Dag Hammarskjold

    Post:24

    Points:168
    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);

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!