* [Caml-list] How to let the user choose a format string?
@ 2011-03-15 18:54 Alain Frisch
0 siblings, 0 replies; only message in thread
From: Alain Frisch @ 2011-03-15 18:54 UTC (permalink / raw)
To: caml-list
Hi all,
I'd like to write a function
val fmt: string -> float -> string
which takes a string, parses it as a format string of type expecting a
single float argument, and applies this format string to a given float.
Expected behavior:
# fmt "%+.2f" 32.;;
- : string = "+32.00"
# fmt "%+.2i";;
--> some exception
I've come up with:
let fmt s = Scanf.sscanf (Printf.sprintf "%S" s) "%{%f%}" Printf.sprintf
which seems to work. Is this indeed supposed to work? Is there a better
way to achieve the same behavior without having to parse the format
string by hand?
Alain
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2011-03-15 18:55 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-15 18:54 [Caml-list] How to let the user choose a format string? Alain Frisch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox