views:

190

answers:

2

I have a variable of double type and the value in it is something like 34.323423423123456 I'd like to format it using String.format method (or whatever works) to have only 2 decimal places (e.g. 34.32), but to also round it properly, e.g. 2.229934345 will be 2.23.

I am new to Java and none the C# tricks worked. Thanks

+4  A: 

Related to this Question use DecimalFormat setRoundingMode

Markus Lausberg
+1  A: 

Here's an example format string, I believe it correctly rounds the resulting value. See Markus' post if you need more control over the rounding or format:

String.format("%.2f", 2.229934345);
Andy White