Floating point maths is a pain in any language as there are limitations to
representing floating points in todays CPU designs.  The problem is that
floats don't fit nicely into 32 or 64 bit systems and floats are represented
as approximations.  I had this problem last year in calculating rainfall
percentiles and wondered why the results where never exactly the same.  Near
enough is good enough it appears.  Having said that IBM did do something in
their hardware to minimize the problem back in 1968.  I don't know of Intel
or AMD used the same nearest number technique.

If you need to be 100% accurate with your cents then I would remove an
decimals by treating your cents as whole numbers separate to your dollars ,
and possibly store as complete cents units.  ie:  (9 * 100) + 95 would be
fine.  You could tokenize the dollars and cents to split it up prior to
turning it into a pure cents value.

I guess most banks have used the slight rounding errors to their advantage.

Cheers
Simon

On 13/08/07, Brett Payne-Rhodes <[EMAIL PROTECTED]> wrote:
>
>
>
> This works in terms of what you are trying to achieve Steve...
>
> <cfset cents = (dollar * 1000)/10 />
>
> Floating point maths in scripted languages has always been a pain...
>
>
> Brett
> B)
>
>
> AJ Mercer wrote:
> > If you do a NumberFormat of cents you get 994.999999999
> > #NumberFormat(cents), '999.99999999999')#
> >
> >
> >
> > On 8/13/07, *Steve Onnis* < [EMAIL PROTECTED]
> > <mailto:[EMAIL PROTECTED]>> wrote:
> >
> >
> >     Its from a shopping cart form
> >
> >     Basically what I wanted to do was stop people sending decimal values
> >     more
> >     than 2 points.  The examples of the implementation I was sent from
> the
> >     provider showed that they were just removing the decimal point from
> the
> >     number value and sending that as the cents value.
> >
> >     Problem is if for some reason the person implementing the credit
> card
> >     processing passed in 9.995 or something.  Then instead of billing
> >     the card
> >     9.99 they would be billed 99.95.
> >
> >     So I figured I would move the point rather than remove it and make
> it a
> >     whole number which is why I was doing this.
> >
> >     I have changed it from FIX() to ROUND() anyway
> >
> >     Steve
> >
> >     -----Original Message-----
> >     From: cfaussie@googlegroups.com <mailto:cfaussie@googlegroups.com>
> >     [mailto: cfaussie@googlegroups.com
> >     <mailto:cfaussie@googlegroups.com>] On Behalf
> >     Of Haikal Saadh
> >     Sent: Monday, 13 August 2007 12:19 PM
> >     To: cfaussie@googlegroups.com <mailto:cfaussie@googlegroups.com>
> >     Subject: [cfaussie] Re: Strange FIX() behaviour
> >
> >
> >     I remember a thread from here a while back discussing the various
> >     idiosyncrasies of floating point arithmetic, and that you should be
> >     really
> >     storing money in cent amounts.
> >
> >     I've got no first hand experience in this, but have a flip thru the
> >     archives.
> >
> >     David Harris wrote:
> >      > Something odd is happening there
> >      >
> >      > If you do this:
> >      >
> >      > <cfset cents = 995 >
> >      > <cfoutput>
> >      >  #FIX(cents)#
> >      > </cfoutput>
> >      >
> >      > you get the expected result. eg: 995
> >      >
> >      > The only thing that springs to mind is that maybe:
> >      >
> >      > 9.95 * 100 = 994.99999999 ?
> >      >
> >      > not sure how to prove this theory tho...
> >      >
> >      >
> >      >
> >      > Steve Onnis wrote:
> >      >
> >      >> Has anyone come across this before?
> >      >>
> >      >> <cfset dollar = 9.95 />
> >      >> <cfset cents = dollar * 100 />
> >      >> <cfoutput>
> >      >> #FIX(cents)#
> >      >> </cfoutput>
> >      >>
> >      >> outputs 994
> >      >>
> >      >> I could maybe understand this is i had more decimal places but
> it is
> >      >> a plain
> >      >> 2 point decimal value.
> >      >>
> >      >> Any ideas?
> >      >>
> >      >>
> >      >>
> >      >>
> >      >>
> >     <
> http://www.inevative.com.au/images/email-stationary/inevativeLogo_sm
> >     <
> http://www.inevative.com.au/images/email-stationary/inevativeLogo_sm>
> >      >> all.jpg
> >      >>
> >      >> Steve Onnis
> >      >>
> >      >>
> >      >> Director / Head Developer
> >      >>
> >      >>
> >      >>   <
> http://www.inevative.com.au/images/email-stationary/email.jpg>
> >      >> [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
> >      >>
> >      >>   <
> >     http://www.inevative.com.au/images/email-stationary/phone.jpg> +61
> >      >> 3 9001
> >      >> 2258
> >      >>
> >      >>
> >     <http://www.inevative.com.au/images/email-stationary/mobile.jpg
> >     <http://www.inevative.com.au/images/email-stationary/mobile.jpg>>
> >      >> 0401 667
> >      >> 996
> >      >>
> >      >>   <http://www.inevative.com.au/images/email-stationary/web.jpg>
> >      >> www.inevative.com.au <http://www.inevative.com.au>
> >      >>
> >      >>  <http://www.novahost.com.au>   <http://www.threesquares.com.au
> >     <http://www.threesquares.com.au>>
> >      >> <http://www.smsonline.com.au>
> >      >>
> >      >
> >      >
> >      > >
> >      >
> >      >
> >
> >
> >
> >
> >
> >
> >     >
>
>
> >
>


-- 
Cheers
Simon Haddon

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"cfaussie" group.
To post to this group, send email to cfaussie@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cfaussie?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to