From: Alain Frisch <alain@frisch.fr>
To: caml-list <caml-list@inria.fr>
Subject: [Caml-list] How to let the user choose a format string?
Date: Tue, 15 Mar 2011 19:54:53 +0100 [thread overview]
Message-ID: <4D7FB5FD.5050106@frisch.fr> (raw)
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
reply other threads:[~2011-03-15 18:55 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4D7FB5FD.5050106@frisch.fr \
--to=alain@frisch.fr \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox