rgrinberg/ocaml-mustache

Float values holding integers are rendered with a trailing period

mfp opened this issue · 4 comments

mfp commented

(version: 2.1.0)

# Mustache.(render (of_string "{{foo}}") @@ `O [ "foo", `Float 3. ]);;
- : string = u"3."

Mustache uses string_of_float, which adds the trailing '.'.

The proper way to render float values is not specified in the mustache (5) manpage, but there's an example which does show an integer being rendered without decimal part, and http://trymustache.com behaves that way too.

Precise control over the formatting requires going through String values, but removing the trailing period for integer values seems sensible.

string_of_float also uses the scientific notation "..e+..* when the float value becomes large. I'm not sure whether we want this or not. One way to get rid of the trailing dot would be to use something likePrintf.sprintf "%.12g", but this also switches to scientific notation for big-enough floats.

If there is no easy way of solving both problem at once, then I can commit the Printf.sprintf "%g" fix, which would still be a strict improvement over the current situation.

Turns out string_of_float is implemented exactly as Printf.sprintf "%.12g" + adding a trailing . when needed. I just pushed a commit that just does the Printf part; this should fix this issue.