简体   繁体   中英

Big Double in Scala templates and Play Framework

I'm using Play Framework 2.3.6 and Scala

When I'm trying to display input with big Double ie 55 000 000 it displays in input 5.5E7

@inputText(field("price"), '_label -> "Price")

<input type="text" id="price" name="price" value="5.5E7">

How can I change default formatting or somehow display it properly?

First of all this is due to the toString representation of the Double value.

For instance

scala> val a: Double = 55000000
a: Double = 5.5E7

scala> a.toString
res8: String = 5.5E7

Double type

According to Scala docs , the Double type is a 64-bit floating point number equivalent to Java's double primitive type.

Now if Double is what you really need, you should format it properly, otherwise you can use Long type.

Formating

You can refer to String Interpolation section in Scala docs for more info on formatting, but briefly, you can achieve what you want with

"%1.0f" format a

which gives you 55000000 as result.

Play helpers

I would say you should customize this where you define your Form (or your Field if you don't have any Form ) and not in the template file.

What you need to change is the format defined in play.api.data.Mapping class :

/**
 * The Format expected for this field, if it exists.
 */
val format: Option[(String, Seq[Any])] = None

in case of defining a Form , or format passed to Field case class in case of using a Field directly:

/**
 * @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]) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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