Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading