Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Rounding up doubles to 2 decimal places

807599Apr 17 2007 — edited Apr 18 2007
Hi i have written a simple project to convert currency which is working fine but i am struggling to figure out how to round the outputs to two decimal places. E.g. 1.563434 would equal 1.56. Here is the program i have written but i do not know where to add the math instruction or which to use as there seems to be many different ways. Thanks

double x;
double y;
double z;
x = Integer.parseInt(txtInput.getText());
y = 0.748279;
z = Double.parseDouble(txtExchange.getText());
if (z > 0)
txtOutput.setText('?' + Double.toString( x * z ));
else
txtOutput.setText('?' + Double.toString( x * y ));
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 16 2007
Added on Apr 17 2007
2 comments
238 views