List Info

Thread: "ocaml_beginners"::[] Formatting float in string_of_float




"ocaml_beginners"::[] Formatting float in string_of_float
country flaguser name
Germany
2007-02-04 22:15:24

Hi,
is it possible to specify a format for floats in string_of_float. E.g.
could I say that I want 5 decimals or e.g. an xxe+/-yy notation like
1.56e23? I know that I can do that with fprintf or sprintf but is it
also possible in string_of_float?

Cheers,
Christian

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Formatting float in string_of_float
country flaguser name
United States
2007-02-05 00:02:15

On Mon, 5 Feb 2007, Christian Lerrahn wrote:

> Hi,
> is it possible to specify a format for floats in string_of_float. E.g.
>; could I say that I want 5 decimals or e.g. an xxe+/-yy notation like
>; 1.56e23? I know that I can do that with fprintf or sprintf but is it
> also possible in string_of_float?

No. What's wrong with sprintf?

--
Martin Jambon
http://martin.jambon.free.fr

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Formatting float in string_of_float
country flaguser name
Germany
2007-02-05 04:37:26

Am Sun, 4 Feb 2007 22:02:15 -0800 (PST)
schrieb Martin Jambon < martin_jambon%40emailuser.net">martin_jambonemailuser.net>:

> On Mon, 5 Feb 2007, Christian Lerrahn wrote:
&gt;
> > Hi,
> > is it possible to specify a format for floats in string_of_float.
&gt; > E.g. could I say that I want 5 decimals or e.g. an xxe+/-yy
> > notation like 1.56e23? I know that I can do that with fprintf or
> > sprintf but is it also possible in string_of_float?
&gt;
> No. What's wrong with sprintf?

I thought sprintf was not type safe whereas string_of_float was but I
found out that sprintf seems to be type safe as well. So I really don't
need string_of_float.

Cheers,
Christian

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Formatting float in string_of_float
country flaguser name
United States
2007-02-05 12:52:04

On Mon, 5 Feb 2007, Christian Lerrahn wrote:

> Am Sun, 4 Feb 2007 22:02:15 -0800 (PST)
&gt; schrieb Martin Jambon < martin_jambon%40emailuser.net">martin_jambonemailuser.net>:
>
&gt; > On Mon, 5 Feb 2007, Christian Lerrahn wrote:
&gt; >
>; > > Hi,
> > > is it possible to specify a format for floats in string_of_float.
&gt; > > E.g. could I say that I want 5 decimals or e.g. an xxe+/-yy
> > > notation like 1.56e23? I know that I can do that with fprintf or
> > > sprintf but is it also possible in string_of_float?
&gt; >
>; > No. What's wrong with sprintf?
>
>; I thought sprintf was not type safe whereas string_of_float was but I
> found out that sprintf seems to be type safe as well. So I really don't
&gt; need string_of_float.

The only dangerous thing with the printf functions is that partial
applications used to not work as expected, but it's not the case anymore.

If I recall correctly, before you would get:

# let f = Printf.printf "Hello %s";;
Helloval f : string -> unit = <fun>;
# f "x&quot;;;
x- : unit = ()

... which was always surprising.

But in ocaml 3.09.3 (I don't know since when exactly but this is
documented somewhere) you get:

# let f = Printf.printf "Hello %s";;
val f : string -> unit = <fun>;
# f "x&quot;;;
Hello x- : unit = ()

That's nicer.

Martin

--
Martin Jambon
http://martin.jambon.free.fr

__._,_.___
.

__,_._,___
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )