2

I have a variable of double type representing a value of about 34.323423423123456. I'd like to format it to have only two decimal places (e.g. 34.32), rounded properly; e.g. 2.229934345 would become 2.23.

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

Pops
  • 30,199
  • 37
  • 136
  • 151
AngryHacker
  • 59,598
  • 102
  • 325
  • 594

2 Answers2

4

Related to this Question use DecimalFormat setRoundingMode

Community
  • 1
  • 1
Markus Lausberg
  • 12,177
  • 6
  • 40
  • 66
2

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
  • 86,444
  • 48
  • 176
  • 211