This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with every Standard Distribution of
Perl.
+
Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?
The infinite set that a mathematician thinks of as the real numbers can
only be approximated on a computer, since the computer only has a finite
number of bits to store an infinite number of, um, numbers.
Internally, your computer represents floatingpoint numbers in binary.
Floatingpoint numbers read in from a file or appearing as literals in
your program are converted from their decimal floatingpoint
representation (eg, 19.95) to an internal binary representation.
However, 19.95 can't be precisely represented as a binary floatingpoint
number, just like 1/3 can't be exactly represented as a decimal
floatingpoint number. The computer's binary representation of 19.95,
therefore, isn't exactly 19.95.
When a floatingpoint number gets printed, the binary floatingpoint
representation is converted back to decimal. These decimal numbers are
displayed in either the format you specify with printf(), or the current
output format for numbers. (See the section on "$#" in the perlvar
manpage if you use print. "$#" has a different default value in Perl5
than it did in Perl4. Changing "$#" yourself is deprecated.)
This affects all computer languages that represent decimal
floatingpoint numbers in binary, not just Perl. Perl provides
arbitraryprecision decimal numbers with the Math::BigFloat module (part
of the standard Perl distribution), but mathematical operations are
consequently slower.
If precision is important, such as when dealing with money, it's good to
work with integers and then divide at the last possible moment. For
example, work in pennies (1995) instead of dollars and cents (19.95) and
divide by 100 at the end.
To get rid of the superfluous digits, just use a format (eg,
"printf("%.2f", 19.95)") to get the required precision. See the section
on "Floatingpoint Arithmetic" in the perlop manpage.

Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short. They represent an important
part of the Usenet tradition. They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.
If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile. If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.
Answers to questions about LOTS of stuff, mostly not related to
Perl, can be found by pointing your news client to
or to the many thousands of other useful Usenet news groups.
Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release. It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.
The perlfaq manual page contains the following copyright notice.
AUTHOR AND COPYRIGHT
Copyright (c) 19971999 Tom Christiansen and Nathan
Torkington. All rights reserved.
This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.