4

I have a table with a Double field, e.g. foo. When I capture numbers like 50 million (50000000) and then I want to view my data, I get: 5.0e7. When I edit the record again, I also view 5.0e7 on the text field.

I can solve the "show" part using a utility function like:

-- Application/Helper/View.hs

module Application.Helper.View where

import Data.Text (unpack)
-- added format-numbers package as dependency on default.nix
import Data.Text.Format.Numbers
import IHP.ViewPrelude


-- Here you can add functions which are available in all your views

moneyConfiguration :: PrettyCfg
moneyConfiguration = PrettyCfg 2 (Just ',') '.'

showMoneyAmount :: Double -> String
showMoneyAmount d = unpack $ prettyF moneyConfiguration d

Then I use it on Web/View/MyRecord/Show.hs as:

   ...
   <div class="col">{get #foo myRecord |> showMoneyAmount}</div>
   ...

But when rendering the form for creating/editing, I can't get rid of the 5.0e7 value:

renderForm :: MyRecord -> Html
renderForm myRecord = formFor myRecord [hsx|
    ...
    {(textField #foo)}
    ...
    {submitButton}
|]

I wish to have something like: {showMoneyAmount <$> (textField #foo)}

or: {(textField #foo){formatValue = showMoneyAmount}}

Any ideas?

Fru Fru
  • 41
  • 3
  • Not sure what you would like to achieve exactly here. Could the [Text.Printf](https://hackage.haskell.org/package/base-4.15.0.0/docs/Text-Printf.html) module be useful ? Try evaluating `pack (printf "%d" (floor 5.0e7))` for example. – jpmarinier Sep 24 '21 at 08:57
  • What is the type of `textField`? – Daniel Wagner Sep 24 '21 at 14:23
  • IHP> :t textField textField :: (?formContext::FormContext model, HasField fieldName model value, HasField "meta" model MetaBag, InputValue value, KnownSymbol fieldName, KnownSymbol (GetModelName model)) => Proxy fieldName -> FormField – Fru Fru Sep 24 '21 at 18:04

1 Answers1

0

Sounds like a bug to me. I just pushed a fix to https://github.com/digitallyinduced/ihp/commit/6545122bf9129cdb53459c116817776f68bcc300

Therefore this should be fixed with the next IHP release

Marc Scholten
  • 1,351
  • 3
  • 5
  • if it gets merged it would be great, but we should be able to choose depending on context, e.g. currency values are often formatted with 2 decimal digits and when displaying, using thousand separator, while when editing, the thousand separator feels uncomfortable – Fru Fru Sep 30 '21 at 16:43