Play-Scala将逗号和小数点添加到Double数据类型 [英] Play-Scala add commas and decimal point to Double datatype
问题描述
我有这个问题,首先是从数据库获取数据以呈现给用户..我已经克服了一些问题,但是我认为这不是最佳实践.这是我的问题:
在我的数据库中,我有一个类型为double(15,2)
的字段,我想以xxx,xxx,xxx.yy
格式显示给用户.目前,如果我只是展示从数据库中获得的信息,它的格式类似于99e10
.是否有最佳实践来实现我想要的?
注意:我目前正在格式化Javascript中的值,当我不断刷新页面时会出现一些故障.
您可以使用java DecimalFormat类来修饰输出
引用:十进制格式 >
在REPL中尝试并检查其是否符合您的期望:
import java.text.DecimalFormat
val d = 9182379812921.12
val f = new DecimalFormat("#,###.00")
f.format(d)
> 9,182,379,812,921.12
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.
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
这篇关于Play-Scala将逗号和小数点添加到Double数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!