Help ! Shopping Cart Problem
- From: Dr John Stockton <jrs@xxxxxxxxxxxxxxxxxx>
- Date: Sun, 30 Oct 2005 12:48:04 +0000
JRS: In article <5vidnaq4xJCLKP7eRVn-qw@xxxxxxxxxxx>, dated Sat, 29 Oct
2005 13:45:09, seen in news:comp.lang.javascript, Randy Webb
<HikksNotAtHome@xxxxxxx> posted :
>
>You are trying to round by multiplying by 100 and then dividing by 100.
>That introduces the error you see because computers cannot represent
>9.95 exactly in Base 2. So you get the error.
Non sequitur.
If the representation of 9.95, while not exact, had sufficient zeroes at
the end of the mantissa, then the multiplication would be exact, and the
division would be exact and would give the original value. There,
"sufficient zeroes" is a sufficient, but probably not entirely
necessary, condition.
If 9.95 being represented inexactly were the only criterion, then ISTM
that one would expect more wrong results than the OP's code actually
gives.
The real problem is that the OP's type of method effectively truncates
to cents using an inexact input; the input must be rounded to exact
cents, which has to be done into a representation where all such values
can be rendered exactly.
The ordinary conversions of a general Number to a String do that
rounding, as does the obvious one of the conversions to an integer Cent
number.
Note that one should always multiply or divide by 100 in preference to
dividing or multiplying by 0.01, since 100 is represented exactly but
0.01 is not.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
The Big-8 newsgroup management is attempting to legitimise its questionable
practices while retaining its elitist hegemony. Read <URL:news:news.groups>.
.
- References:
- Help ! Shopping Cart Problem
- From: Phil McKraken
- Re: Help ! Shopping Cart Problem
- From: RobG
- Re: Help ! Shopping Cart Problem
- From: Phil McKraken
- Help ! Shopping Cart Problem
- Prev by Date: stuck on how to access a textfield added in a div
- Next by Date: Help ! Shopping Cart Problem
- Previous by thread: Re: Help ! Shopping Cart Problem
- Next by thread: Re: Help ! Shopping Cart Problem
- Index(es):
Relevant Pages
|