cookieWarrior - 5 months ago 19

Java Question

How can I display only 2 decimal places from the inputted answer? when I do this the answer is different

`Scanner user_input = new Scanner(System.in);`

float num;

DecimalFormat f = new DecimalFormat("##.00");

System.out.println("Enter any number");

num = user_input.nextFloat();

System.out.println("Square of "+num+" is: "+f.format(num)+Math.pow(num,2));

}

Answer

You need to sum the power with the answer and then format the result. Currently you are formatting the number and afterwards performing the power function causing the result to be in raw format.

Modify your output line to:

```
System.out.println("Square of "+num+" is: "+f.format(num+Math.pow(num,2)));
```

So for an input of 21.12345 the result would look like:

```
Enter any number 21.12345
Square of 21.12345 is: 467.32
```