0 votes
in JAVA by

How to round up to 2 decimal places in java?

I have read a lot of stackoverflow questions but none seems to be working for me. i am using math.round() to round off. this is the code:

class round{

    public static void main(String args[]){

    double a = 123.13698;

    double roundOff = Math.round(a*100)/100;

    System.out.println(roundOff);

}

}

the output i get is: 123 but i want it to be 123.14. i read that adding *100/100 will help but as you can see i didn't manage to get it to work.

it is absolutely essential for both input and output to be a double.

it would be great great help if you change the line 4 of the code above and post it.

1 Answer

0 votes
by

Well this one works...

double roundOff = Math.round(a * 100.0) / 100.0;

Output is

123.14

Or 

 double roundOff = (double) Math.round(a * 100) / 100;

this will do it for you as well.

Related questions

+2 votes
asked May 31, 2020 in JAVA by SakshiSharma
0 votes
asked Apr 1, 2022 in SAP S/4HANA Key Concepts Overview by Robindeniel
...