Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8646935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:56:22+00:00 2026-06-12T12:56:22+00:00

Perhaps I’m just overlooking something obvious but I can’t figure out how to bind

  • 0

Perhaps I’m just overlooking something obvious but I can’t figure out how to bind a form field to a double in a Play controller.

For instance, assume this is my model:

case class SavingsGoal(
timeframeInMonths: Option[Int],
amount: Double,
name: String
)

(Ignore that I’m using a double for money, I know that’s a bad idea, this is just a simplified example)

And I wanted to bind it like so:

object SavingsGoals extends Controller {

    val savingsForm: Form[SavingsGoal] = Form(

        mapping(
            "timeframeInMonths" -> optional(number.verifying(min(0))),
            "amount" -> of[Double],
            "name" -> nonEmptyText
        )(SavingsGoal.apply)(SavingsGoal.unapply)

    )

}

I realized that the number helper only works for ints but I thought using of[] might allow me to bind a double. However, I get a compiler error on this:

Cannot find Formatter type class for Double. Perhaps you will need to import
play.api.data.format.Formats._  

Doing so doesn’t help as there’s no double formatter defined in the API.

This is all just a long way of asking what’s the canonical way of binding a form field to a double in Play?

Thanks!

edit: 4e6 pointed me in the right direction. Here’s what I did using his help:

Using the snippets in his link, I added the following to app.controllers.Global.scala:

object Global {

    /**
     * Default formatter for the `Double` type.
     */
    implicit def doubleFormat: Formatter[Double] = new Formatter[Double] {

      override val format = Some("format.real", Nil)

      def bind(key: String, data: Map[String, String]) =
        parsing(_.toDouble, "error.real", Nil)(key, data)

      def unbind(key: String, value: Double) = Map(key -> value.toString)
    }

    /**
     * Helper for formatters binders
     * @param parse Function parsing a String value into a T value, throwing an exception in case of failure
     * @param error Error to set in case of parsing failure
     * @param key Key name of the field to parse
     * @param data Field data
     */
    private def parsing[T](parse: String => T, errMsg: String, errArgs: Seq[Any])(key: String, data: Map[String, String]): Either[Seq[FormError], T] = {
      stringFormat.bind(key, data).right.flatMap { s =>
        util.control.Exception.allCatch[T]
          .either(parse(s))
          .left.map(e => Seq(FormError(key, errMsg, errArgs)))
      }
    }

}

Then, in my form mapping:

mapping(
    "amount" -> of(Global.doubleFormat)
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:56:23+00:00Added an answer on June 12, 2026 at 12:56 pm

    Actually, there is predefined formatter for Double on master branch. So you should either switch to 2.1-SNAPSHOT play version or just copy the implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Perhaps I'm just having trouble figuring out the callbackyness, but I can't figure out
Perhaps I'm overlooking something obvious but I was wondering what the fastest way to
Perhaps I am missing something, but I am just learning javascript. My understanding of
Perhaps I'm missing something obvious here, but I'm having a hard time setting a
Perhaps I am missing something obvious, but when I utilize the dojo date picker
Perhaps i am being stupid by asking this just now, but... I notice that
Perhaps I'm missing something here, but it seems that anything in the object model
Perhaps I'm missing something here, but I want the user to be able to
Perhaps this is answered out there somewhere and I just don't know the words
Perhaps I'm missing something but when I scan an NFC tag via a galaxy

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.