sprintf doesn't round correctly?

G

Guest

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?
 
C

Chris Haffenstedt

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
 
J

Jürgen Exner

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
 
J

Joost Diepenmaat

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
 
G

Guest

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


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.
 
J

Jürgen Exner

Joost Diepenmaat said:
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
 
T

Tim Greer

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top