sgrgmngt
sgrgmngt

Reputation: 917

Decimalformat in java

I am using decimalformat to format a value to 2 decimal places

Ex : 200.0075 --> 200.00
Ex:  200.0050 --> 200.00

The code i am using is :

DecimalFormat df = new DecimalFormat("#.##")

Problem is

String value = df.format(200.0075) gives 200.01

and

String value = df.format(200.0050) gives 200.00

How can i get only 2 decimal places using deciamlformat in java.

Upvotes: 0

Views: 238

Answers (1)

Matt Ball
Matt Ball

Reputation: 360046

You're seeing this rounding behavior because the default rounding mode for DecimalFormat is HALF_EVEN. Use RoundingMode.DOWN instead.

Upvotes: 1

Related Questions