0
    public static void main(String[] args) {//begin main
    Scanner worksheet = new Scanner(System.in);
       {
        double radi;
        double area;
        double circum;
        double pie = 3.14;

        System.out.println("Enter the radius: ");
        radi = worksheet.nextDouble();//enter 1.2 

        area = pie * radi * radi;

Area comes out as 4.521599999999999, but I want it to be 4.52.

        System.out.println("The area is:" + area);

        circum=2*pie*radi;

circum comes out as 7.536. want it to be 7.53

        System.out.println("The circumference is:"+circum);

    }

How can I truncate the value to 2 decimal places without a method.

payloc91
  • 3,724
  • 1
  • 17
  • 45

1 Answers1

0

You can try

DecimalFormat df = new DecimalFormat("##.##");
df.setRoundingMode(RoundingMode.DOWN);
System.out.println("The circumference is:"+df.format(circum));

or you can simply do this

int temp = (int)(circum*100);//452
circum = temp/100d;//4.52

Both of these will limit the value up to 2 decimal points.

Wish it will solve the problem.

Reference (Take a look at this)

Hamza Anis
  • 2,475
  • 1
  • 26
  • 36