JRS: In article <
[email protected]>, seen in
Cardman <
[email protected]> posted at Tue, 3
Feb 2004 23:04:36 :-
(VAT stuff) : I was not addressing your situation, but the defects of
your answer to Chiwa's original question, and the probable context of
that question. When responding to an article in News, it is wise to
examine the position of that article in its thread, and at least to
review its immediate predecessors.
The need is to display the correct answer, where that is being done
string or no string.
Numbers cannot be displayed; only strings can. On output of a
javascript number, there is an automatic conversion to string.
Consider document.write(3.00) - the result will be 3, not
3.00, and that is correct by language definition.
The usual reason for wanting to truncate a Number to a multiple of 0.01
is that a currency calculation with input sums to two places gives a
result with rounding errors which displays something like 3.00000000001;
"numeric truncation" and "numeric rounding" both give a Number of 3,
displaying as 3. Those who use numeric truncation eventually discover
that they need to change to numeric rounding, in order not to lose one
unit up to half the time.
But they then see that sums of money such as three pounds, three pounds
five pence, and three pounds ten pence display as 3 3.05 3.1, whereas
convention demands 3.00, 3.05, 3.10. To get those in javascript the
Number must be converted to a String with two decimal places before
output.
The conversion needs a function.
There is no need to round the number before the function; it is better
to do it within.
I see a possible problem with your VAT approach. VAT here is 17.5%.
In any language using IEEE Doubles,
1.175 - 1.0 - 0.125 = 0.050000000000000044
0.175 - 0.125 = 0.04999999999999999
Here 1.0 and 0.125 are numbers that an IEEE Double can represent
exactly. So 1.175 is stored as slightly more, but 0.175 as slightly
less.
Thus for ten items costing GBP 1.00 exc. VAT, with truncation, the
customer may be called on to pay GBP 11.75, and the VATman may be
offered GBP 1.74. Accounts may show a discrepancy of one penny.
Amusing thought : % means /per centum/. Literally, a VAT rate of 17.5%
means not that the tax is the net times 0.175, but that for every 100
pence in the net the tax is 17.5 pence --- and 17.5 can of course be
represented exactly. If the calculation had been done literally,
calculating the pennies of VAT from the hundreds-of-pence net, the
results would have been exact.