Home > Blockchain >  Decimal Format Value customisation issue
Decimal Format Value customisation issue

Time:01-07

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 ?

CodePudding user response:

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")
  •  Tags:  
  • Related