Re: sprintf - Rounding down



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
.



Relevant Pages

  • Re: Bug in Delphi SimpleRoundTo function?
    ... Ask any mathematician and you'll get the same answer: 0.5 rounds up. ... Banker's Rounding is a convenience to avoid either party ... the trap of using floating *binary* point numbers, ...
    (borland.public.delphi.non-technical)
  • Re: sprintf - Rounding down
    ... On 15 Mar 2007 at 8:05, Tom Phoenix wrote: ... with my formatting below. ... The %f is floating, so it rounds. ...
    (perl.beginners)
  • Re: 2 decimals with currency
    ... The function, I would presume, is run in order to display the value. ... you may need to apply formatting to your form ... "Abrm" wrote in message ... it rounds 70,7 ...
    (microsoft.public.access.modulesdaovba)
  • Re: Rounding to thousands in excel
    ... What rounding rules are you proposing? ... your third example rounds UP. ... there's no way to do that just with formatting. ... If the number is 49,677,298.86 I want 49,600 to display ...
    (microsoft.public.excel.misc)
  • Re: Rounding Dollar Amounts
    ... LineTaxExempt field is checked (meaning tax exempt), ... This works most of the time, but I have run into a few rounding errors. ... With tax exempt checked (and multiple lines) ... By "rounds wrong", I mean I ...
    (comp.databases.ms-access)