I definitely understand that certain operations used on an ecommerce site
will give pesky floats, as you mentioned:
As it's an eCommerce site I'm putting together, I need the maths to add up
precisely on the basket screen etc. I have got some functions that will
inevitably return pesky floats (adding 17.5% tax, currency conversions etc).
But rather than change your formatting and rounding techniques, I would
suggest adding the total in whatever currency the prices were originally
given in, and then multiplying that total by the exchange rate and tax
percentages. That way, you will not need to worry about adding fractions of
a cent (or whatever currency is being used); you can simply round or
concatenate the decimal places one time for the total rather doing it for
every item. Example (my conversion & tax factors may be wrong here):
Cost Conv Tax
£15.99 * 1.76 * 17.5% = 33.06732 = 33.06
£10.50 * 1.76 * 17.5% = 21.714 = 21.71
£ 8.27 * 1.76 * 17.5% = 17.10236 = 17.10
71.88368 = 71.88 (This is of by
..01)
OR
Cost Conv Tax
£15.99
£10.50
£ 8.27
£34.76 * 1.76 * 17.5% = 71.88368 = 71.88 (This does not show any unexpected
decimals)
If only some items are taxed, you may need to add up the taxable and
non-taxable items separately, but otherwise the same. You may want to
display the operations differently on the site, and I am not experienced in
economics or finance, but I usually see tax and conversions done at the end,
which I believe makes it simpler for both the user and in the code. Just a
suggestion.
--
Nathan Sokalski
njsokalski@hotmail.com www.nathansokalski.com
"Ted" <spam@spam.com> wrote in message
news:uU9jcpBcEHA.3904@TK2MSFTNGP12.phx.gbl...[color=blue][color=green]
> > Don't know why you need fix or int here though. These work fine for me:
> >
> > response.write(FormatNumber((2.30 * 100)/100.0, 2))
> > response.write(FormatPercent((2.30)/100.0, 2))[/color]
>
> The reason I am using fix() is so: -
>
> response.write(FormatNumber((fix(2.309 * 1 * 100) / 100) , 2))
>
> will result in 2.30
>
> Maybe I'm goint to have to re-think my appraoch to this problem.
>
> As it's an eCommerce site I'm putting together, I need the maths to add up
> precisely on the basket screen etc. I have got some functions that will
> inevitably return pesky floats (adding 17.5% tax, currency conversions[/color]
etc).[color=blue]
>
> What I was doing was chopping off everthing after the second decimal place
> in all of my calculations using the function I mentioned. This, until now,
> had worked OK and all the totals had added up. Until I got the dreaded[/color]
2.30[color=blue]
> resulting in 2.29.
>
> Any ideas how I should tackle this problem?
>
> Thanks for the reply,
>
> Ted.
>[color=green][color=darkred]
> > > I've written a little function to remove everything after the 2nd[/color][/color]
> decimal[color=green][color=darkred]
> > > place for prices which is as follows: -
> > >
> > > ReturnConvertedCurrency = (fix(iSterling *[/color][/color]
> session("ExchangeRate")[color=green]
> > *[color=darkred]
> > > 100) / 100)
> > >
> > > However, it sometimes returns incorrect values. i.e. Why does the
> > > following: -
> > >
> > > response.write(FormatNumber((fix(2.30 * 1 * 100) / 100) , 2))
> > >
> > > or
> > >
> > > response.write(FormatNumber((int(2.30 * 1 * 100) / 100) , 2))
> > >
> > > return 2.29 ?
> > >
> > > Any help would be greatly appreciated as I've been ripping my hair out[/color][/color]
> for[color=green]
> > a[color=darkred]
> > > few hours trying to solve this one.
> > >
> > > Ted.
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]