Play-Scala add commas and decimal point to Double

2019-08-28 21:46发布

问题:

I have this problem starting from getting data from DB to present it to the user.. I have overcame some issues, but I don't think it's a best practice.. Here is my problem:

In my DB, I have a field with type double(15,2), I want to show it to the user in this format xxx,xxx,xxx.yy. Currently if I just show what I got from the DB, it's in something like this format 99e10.. Is there any best practice to achieve what I want ?

NOTE: I'm currently formatting the value from Javascript which gives some glitches when I continuously refresh the page.

回答1:

You can use java DecimalFormat class to pretty your output

Refer : Decimal Format

Try it in REPL and check it fit your expectations:

import java.text.DecimalFormat
val d = 9182379812921.12
val f = new DecimalFormat("#,###.00")
f.format(d)
> 9,182,379,812,921.12