From: Michel Quercia <quercia@cal.enst.fr>
To: caml-list@Montchapet.ecole
Subject: Re: How to format a float?
Date: Tue, 26 Oct 1999 00:53:47 +0000 [thread overview]
Message-ID: <99102601064300.03249@Montchapet> (raw)
In-Reply-To: <3813AA4E.7016C100@maxtal.com.au>
Le lun, 25 oct 1999, vous avez écrit :
: How do I format a floating point number
: correctly in ocaml, with a given _variable_
: width and precision?
Like this :
let str width prec =
let prec = min 8 (max prec 0) in
let pow_ten = 10.0 ** float(prec) in
fun x ->
let y = abs_float(x) in
let sgn= if x < 0. then -1. else 1. in
let a = floor(y) in
let b = 1. +. y -. a in
let z = truncate(b*.pow_ten +. 0.5) in
let sa = string_of_float(sgn*.a) in
let sb = string_of_int(z) in
let pad= width - String.length(sa) - prec - 1 in
(String.make (max 0 pad) ' ') ^ sa ^ "." ^ (String.sub sb 1 prec)
The "8" in "min 8 (max prec 0)" is here to avoid integer overflow when
computing "z" on 32-bit computers. "b" is 1 too large in order to force
"string_of_int(z)" having "prec+1" characters.
Not very nice ... Perhaps an addition to the standard printf library
routines in order to accept the "*" variable field length would be better.
--
Michel Quercia
9/11 rue du grand rabbin Haguenauer, 54000 Nancy
http://pauillac.inria.fr/~quercia
mailto:quercia@cal.enst.fr
next prev parent reply other threads:[~1999-10-26 6:42 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-10-15 13:53 localization, internationalization and Caml Gerard Huet
1999-10-15 20:28 ` Gerd Stolpmann
1999-10-19 18:06 ` skaller
1999-10-20 21:05 ` Gerd Stolpmann
1999-10-21 4:42 ` skaller
1999-10-21 12:05 ` Matías Giovannini
1999-10-21 15:35 ` skaller
1999-10-21 16:27 ` Matías Giovannini
1999-10-21 16:36 ` skaller
1999-10-21 17:21 ` Matías Giovannini
1999-10-23 9:53 ` Benoit Deboursetty
1999-10-25 21:06 ` Jan Skibinski
1999-10-26 18:02 ` skaller
1999-10-25 0:54 ` How to format a float? skaller
1999-10-26 0:53 ` Michel Quercia [this message]
1999-10-26 4:36 ` Go for ultimate localization! Benoit Deboursetty
1999-10-28 17:04 ` Pierre Weis
1999-10-28 17:41 ` Matías Giovannini
1999-10-28 17:59 ` Matías Giovannini
1999-10-29 9:44 ` Francois Pottier
1999-10-28 21:00 ` Gerd Stolpmann
1999-10-29 4:29 ` skaller
1999-10-17 14:29 ` localization, internationalization and Caml Xavier Leroy
1999-10-19 18:36 ` skaller
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=99102601064300.03249@Montchapet \
--to=quercia@cal.enst.fr \
--cc=caml-list@Montchapet.ecole \
/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;
as well as URLs for NNTP newsgroup(s).