Jaimin Modi
Jaimin Modi

Reputation: 1667

Decimal Format Value customisation issue

I am using below code to convert 79.00538 to 79.00

val df = DecimalFormat("###.##")
df.roundingMode = RoundingMode.DOWN
val retrunValue= df.format(79.00538)

But it give me 79 instead of 79.00.

What might be the issue ?

Upvotes: 0

Views: 64

Answers (1)

Ma3x
Ma3x

Reputation: 6549

DecimalFormat docs state

Symbol  Location    Localized?  Meaning
----------------------------------------------------------- 
0       Number      Yes         Digit 
#       Number      Yes         Digit, zero shows as absent 

So # is a digit, but when it is zero, it will not be displayed. Use 0 for places that you want displayed even when zero.

So I am assuming you wanted a format like this

val df = DecimalFormat("0.00")

Or possibly a format like this, if you want 0.1234 to display as .12

val df = DecimalFormat("#.00")

Upvotes: 1

Related Questions