Java: How do I set the decimal place of a double to 2 places.

Started by
2 comments, last by Themonkster 21 years ago
Hey guys. need help this.price = (Math.random()*100); this comes out 8.1234567890123456 I want 8.12 I am stumped must be pretty simple but none of the books I have seem to cover it. odd. Cheers
Advertisement
In Java? Not sure, but a general way is:

this.price = Math.floor(Math.random() * 100 * 100) / 100;

Use a DecimalFormat object.
excellent cheers guys both methods worked a treat.

This topic is closed to new replies.

Advertisement