我正在使用 Play Framework 2.3.6 和 Scala
当我尝试使用大 Double 显示输入时,即 55 000 000 它显示在输入 5.5E7
@inputText(field("price"), '_label -> "Price")
<input type="text" id="price" name="price" value="5.5E7">
如何更改默认格式或以某种方式正确显示它?
我正在使用 Play Framework 2.3.6 和 Scala
当我尝试使用大 Double 显示输入时,即 55 000 000 它显示在输入 5.5E7
@inputText(field("price"), '_label -> "Price")
<input type="text" id="price" name="price" value="5.5E7">
如何更改默认格式或以某种方式正确显示它?
首先,这是由于值的toString表示Double。
例如
scala> val a: Double = 55000000
a: Double = 5.5E7
scala> a.toString
res8: String = 5.5E7
Double类型
根据Scala 文档,该Double类型是一个 64 位浮点数,相当于 Java 的double原始类型。
现在如果Double是你真正需要的,你应该正确格式化它,否则你可以使用Longtype.
成型
您可以参考Scala 文档中的字符串插值部分以获取有关格式化的更多信息,但简而言之,您可以通过
"%1.0f" format a
这给了你55000000结果。
玩帮手
我会说你应该在你定义你的Form(或者你的Field,如果你没有的话Form)而不是在模板文件中自定义它。
您需要更改的是类中format定义的play.api.data.Mapping:
/**
* The Format expected for this field, if it exists.
*/
val format: Option[(String, Seq[Any])] = None
在定义 a 的情况下Form,或者在直接使用 a 的情况下format传递给Field案例类Field:
/**
* @param format the format expected for this field
*/
case class Field(private val form: Form[_], name: String, constraints: Seq[(String, Seq[Any])], format: Option[(String, Seq[Any])], errors: Seq[FormError], value: Option[String])