Re: sprintf - Rounding down
- From: tom@xxxxxxxxxxxxxx (Tom Phoenix)
- Date: Thu, 15 Mar 2007 08:05:36 -0800
On 3/15/07, Beginner <dermot@xxxxxxxxxxxxxxxx> wrote:
For some reason my sprintf usage is not returning the numbers I had
expected and appears to be rounding down. Is there something wrong
with my formatting below.
my $ksize = sprintf("%.2f",$size/1024);
my $mbsize = sprintf("%.2f",$ksize/1024);
my $msize = sprintf("%d",$mbsize);
The %d format is for integers, so it always truncates any fractional
part. The %f is floating, so it rounds. If you want to round to the
nearest integer, then, use a format like "%.0f" or "%6.0f".
Does that solve your problem? The sprintf documentation in the
perlfunc manpage is quite extensive.
Cheers!
--Tom Phoenix
Stonehenge Perl Training
.
- Follow-Ups:
- Re: sprintf - Rounding down
- From: Beginner
- Re: sprintf - Rounding down
- References:
- sprintf - Rounding down
- From: Beginner
- sprintf - Rounding down
- Prev by Date: help with LWP credentials
- Next by Date: Re: Sys::Gamin install error
- Previous by thread: sprintf - Rounding down
- Next by thread: Re: sprintf - Rounding down
- Index(es):
Relevant Pages
|