Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jonathan Bryant <jtbryant@valdosta.edu>
To: "Quôc Peyrot" <chojin@lrde.epita.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] precision not working properly for strings in Printf?
Date: Tue, 26 Jun 2007 23:38:02 -0400	[thread overview]
Message-ID: <7A72D27B-4FF3-4483-8D42-B9904FA734E2@valdosta.edu> (raw)
In-Reply-To: <0AC5F6BF-D076-4561-B015-70E41954D248@lrde.epita.fr>


On Jun 26, 2007, at 11:18 PM, Quôc Peyrot wrote:

> It seems that the precision field doesn't work properly with the  
> "s" type in Printf.printf:

It does work properly.  It's just not working the way you're  
expecting it to work.

>
> # Printf.printf "%.2s" "qwerty";;
> qwerty- : unit = ()
>
> This should print "qw"

No it shouldn't.  The width/precision specifier guarantees a  
_minimum_ length for strings, not an absolute length.  From the docs  
for Printf:

"The optional width is an integer indicating the minimal width of the  
result. For instance, %6d prints an integer, prefixing it with spaces  
to fill at least 6 characters.

The optional precision is a dot . followed by an integer indicating  
how many digits follow the decimal point in the %f, %e, and %E  
conversions. For instance, %.4f prints a float with 4 fractional  
digits."

Minimal length for width and precision doesn't apply to string  
conversions.  Here's how it works:

         Objective Caml version 3.09.3

# Printf.sprintf "%2s" "qwerty";;
- : string = "qwerty"

# Printf.sprintf "%.2s" "qwerty";;
- : string = "qwerty"

# Printf.sprintf "%5s" "a";;
- : string = "    a"

# Printf.sprintf "%.5s" "a";;
- : string = "    a"

Hope that helps.

--Jonathan

>
> -- 
> Best Regards,
> Quôc
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


  reply	other threads:[~2007-06-27  3:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-27  3:18 Quôc Peyrot
2007-06-27  3:38 ` Jonathan Bryant [this message]
2007-06-27  3:48   ` [Caml-list] " Quôc Peyrot
2007-06-27  4:11     ` Jonathan Bryant
2007-06-27  8:34       ` Vincent Hanquez
2007-06-27  9:53         ` Jonathan Bryant
2007-06-27 11:08           ` Quôc Peyrot
2007-07-03  1:14           ` David Thomas
2007-06-27 10:46         ` Quôc Peyrot

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=7A72D27B-4FF3-4483-8D42-B9904FA734E2@valdosta.edu \
    --to=jtbryant@valdosta.edu \
    --cc=caml-list@inria.fr \
    --cc=chojin@lrde.epita.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