Interesting. Any explanation why sprintf treats 3.5 and 4.5 differently in 
rounding?

Thanks Bill.

-Eugene


-----Original Message-----
From: Bill Luebkert [mailto:[EMAIL PROTECTED]
Sent: Friday, March 16, 2007 5:33 PM
To: Su, Yu (Eugene)
Cc: [email protected]
Subject: Re: how to round a floating-point value to an integer value


Su, Yu (Eugene) wrote:
> I expect 
> print sprintf("%.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f", 0.5, 1.5, 
> 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5);
> will give me 
> 1 2 3 4 5 6 7 8 9 10
> but instead I get 
> 0 2 2 4 4 6 6 8 8 10
> 
> How to round a floating-point value to an integer value using (.5) up rule?

I'd add .5 in the next place down :

foreach (0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0) {
        printf "%.1f => %.0f\n", $_, $_ + .05;
}

0.0 => 0
0.1 => 0
0.2 => 0
0.3 => 0
0.4 => 0
0.5 => 0
0.6 => 1
0.7 => 1
0.8 => 1
0.9 => 1
1.0 => 1

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to