Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > sprintf doesn't round correctly?

Reply
Thread Tools

sprintf doesn't round correctly?

 
 
void.no.spam.com@gmail.com
Guest
Posts: n/a
 
      09-27-2008
Hi, I have the following code, where the multiplication results in a
dollar amount which needs to be rounded to the nearest cent.

$a = sprintf("%.2f", 1900*18.12895);
print $a;

The multiplication yields the value 34445.005, which should be rounded
up to 34445.01, but perl says that $a has the value 34445.00. Why is
it exhibiting this behavior?
 
Reply With Quote
 
 
 
 
Chris Haffenstedt
Guest
Posts: n/a
 
      09-27-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) schrieb:
> Hi, I have the following code, where the multiplication results in a
> dollar amount which needs to be rounded to the nearest cent.
>
> $a = sprintf("%.2f", 1900*18.12895);
> print $a;
>
> The multiplication yields the value 34445.005, which should be rounded
> up to 34445.01, but perl says that $a has the value 34445.00. Why is
> it exhibiting this behavior?


sprintf doesn't round at all. In your example, you get the result of
your multiplication, and after that, sprintf gives you the result with
the two digits after the ".".
More about rounding in Perl:
http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
 
Reply With Quote
 
 
 
 
void.no.spam.com@gmail.com
Guest
Posts: n/a
 
      09-27-2008
On Sep 27, 3:38*pm, Chris Haffenstedt <(E-Mail Removed)> wrote:
> (E-Mail Removed) schrieb:
>
> > Hi, I have the following code, where the multiplication results in a
> > dollar amount which needs to be rounded to the nearest cent.

>
> > $a = sprintf("%.2f", 1900*18.12895);
> > print $a;

>
> > The multiplication yields the value 34445.005, which should be rounded
> > up to 34445.01, but perl says that $a has the value 34445.00. *Why is
> > it exhibiting this behavior?

>
> sprintf doesn't round at all. In your example, you get the result of
> your multiplication, and after that, sprintf gives you the result with
> the two digits after the ".".
> More about rounding in Perl:http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html


That is not true, because I tried this code:

$a = sprintf("%.2f", 3.0051);
print $a;

And it printed out 3.01
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      09-27-2008
"(E-Mail Removed)" <(E-Mail Removed)> wrote:
>Hi, I have the following code, where the multiplication results in a
>dollar amount which needs to be rounded to the nearest cent.
>
>$a = sprintf("%.2f", 1900*18.12895);
>print $a;
>
>The multiplication yields the value 34445.005,


No, it doesn't. At least not on a typical computer.

>which should be rounded
>up to 34445.01, but perl says that $a has the value 34445.00. Why is
>it exhibiting this behavior?


You must have missed the introductory lesson in the Introduction into
Computer Numerics class.

Try
$a = sprintf("%.20f", 1900*18.12895);
print $a;
and the reason will become obvious (note: 2 replaced with 20 digits).

For a more detailed explanation please see 'perldoc -q 9999'.

jue
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      09-27-2008
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> Hi, I have the following code, where the multiplication results in a
> dollar amount which needs to be rounded to the nearest cent.
>
> $a = sprintf("%.2f", 1900*18.12895);
> print $a;
>
> The multiplication yields the value 34445.005, which should be rounded
> up to 34445.01, but perl says that $a has the value 34445.00. Why is
> it exhibiting this behavior?


The direction of rounding at exactly x.yx5 is debatable. Perl uses
banker's rounding, which rounds towards even:

printf("%1.0f",0.5) => "0"
printf("%1.0f",1.5) => "2"

There are good reasons why it does this for general rounding: the last
time we had this question it turned into a rediculously long discussion.

Just see http://en.wikipedia.org/wiki/Rounding

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
 
Reply With Quote
 
void.no.spam.com@gmail.com
Guest
Posts: n/a
 
      09-27-2008
On Sep 27, 3:46*pm, Jürgen Exner <(E-Mail Removed)> wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> wrote:
> >Hi, I have the following code, where the multiplication results in a
> >dollar amount which needs to be rounded to the nearest cent.

>
> >$a = sprintf("%.2f", 1900*18.12895);
> >print $a;

>
> >The multiplication yields the value 34445.005,

>
> No, it doesn't. At least not on a typical computer.
>
> >which should be rounded
> >up to 34445.01, but perl says that $a has the value 34445.00. *Why is
> >it exhibiting this behavior?

>
> You must have missed the introductory lesson in the Introduction into
> Computer Numerics class.
>
> Try
> * * * * $a = sprintf("%.20f", 1900*18.12895);
> * * * * print $a;
> and the reason will become obvious (note: 2 replaced with 20 digits).
>
> For a more detailed explanation please see 'perldoc -q 9999'.
>
> jue


Ah, that explains it. Thanks.
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      09-27-2008
Joost Diepenmaat <(E-Mail Removed)> wrote:
>"(E-Mail Removed)" <(E-Mail Removed)> writes:
>
>> Hi, I have the following code, where the multiplication results in a
>> dollar amount which needs to be rounded to the nearest cent.
>>
>> $a = sprintf("%.2f", 1900*18.12895);
>> print $a;
>>
>> The multiplication yields the value 34445.005, which should be rounded
>> up to 34445.01, but perl says that $a has the value 34445.00. Why is
>> it exhibiting this behavior?

>
>The direction of rounding at exactly x.yx5 is debatable. Perl uses
>banker's rounding, which rounds towards even:


This may or may not be the case. Either way it is not the reason for the
OPs problem. He doesn't have a trailing '5' do being with.

jue
 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      09-27-2008
(E-Mail Removed) wrote:

> Hi, I have the following code, where the multiplication results in a
> dollar amount which needs to be rounded to the nearest cent.
>
> $a = sprintf("%.2f", 1900*18.12895);
> print $a;
>
> The multiplication yields the value 34445.005, which should be rounded
> up to 34445.01, but perl says that $a has the value 34445.00. Why is
> it exhibiting this behavior?


See:

perldoc -q decimals
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
Martien Verbruggen
Guest
Posts: n/a
 
      09-27-2008
On Sat, 27 Sep 2008 21:38:26 +0200,
Chris Haffenstedt <(E-Mail Removed)> wrote:
> (E-Mail Removed) schrieb:
>> Hi, I have the following code, where the multiplication results in a
>> dollar amount which needs to be rounded to the nearest cent.
>>
>> $a = sprintf("%.2f", 1900*18.12895);
>> print $a;

>
> sprintf doesn't round at all.


sprintf and printf do rounding.

See

# perldoc -q round

and

# perldoc -f sprintf
(search for 'rounded')

Martien
--
|
Martien Verbruggen | "In a world without fences,
| who needs Gates?"
|
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
So, round and round... 70-682... MSLearning, please read! DaGenester MCITP 2 05-20-2010 04:36 PM
reading file round and round cerr Perl Misc 6 03-20-2010 11:37 AM
VS2005 - Going round and round in circles again :) postings@alexshirley.com ASP .Net 0 06-22-2006 11:26 AM
Float.round - should it be round-to-even OliverMarchand Ruby 2 04-12-2006 12:06 PM
Computer Goes Round and Round Checking Alan Computer Support 6 10-08-2004 08:24 AM



Advertisments