简体   繁体   English

Play-Scala将逗号和小数点添加到Double数据类型

[英]Play-Scala add commas and decimal point to Double datatype

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 . 在我的数据库中,我有一个类型为double(15,2)的字段,我想以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 ? 目前,如果仅显示从数据库中获得的信息,则格式为99e10 ..是否有最佳实践来实现我想要的?

NOTE: I'm currently formatting the value from Javascript which gives some glitches when I continuously refresh the page. 注意:我当前正在格式化Javascript中的值,这在我不断刷新页面时会出现一些故障。

You can use java DecimalFormat class to pretty your output 您可以使用Java DecimalFormat类来修饰输出

Refer : Decimal Format 请参阅: 十进制格式

Try it in REPL and check it fit your expectations: 在REPL中尝试并检查它是否符合您的期望:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM