From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p2FItTIj011192 for ; Tue, 15 Mar 2011 19:55:29 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArMCAFxTf03B/BfUc2dsb2JhbACmHQEMCgwHFCXCQYViBIxdg08 X-IronPort-AV: E=Sophos;i="4.63,189,1299452400"; d="scan'208";a="78199810" Received: from msa03.smtpout.orange.fr (HELO msa.smtpout.orange.fr) ([193.252.23.212]) by mail3-smtp-sop.national.inria.fr with ESMTP; 15 Mar 2011 19:55:02 +0100 Received: from [192.168.1.63] ([83.199.55.106]) by mwinf5d26 with ME id KWv01g00V2HWBDP03Wv1WL; Tue, 15 Mar 2011 19:55:01 +0100 Message-ID: <4D7FB5FD.5050106@frisch.fr> Date: Tue, 15 Mar 2011 19:54:53 +0100 From: Alain Frisch User-Agent: Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 MIME-Version: 1.0 To: caml-list Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: [Caml-list] How to let the user choose a format string? 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