The problem is called Cancellation in Floating Point arithmetic. It occurs
in the subtraction between nearly equivalent operands. If the string can be
represented by a real number, here is a solution I've written for you:

function parseReal(a:String){
    var b = a.split('.'), l=b[1].length, b=[Number(b[0]), Number(b[1])], c =
b[1]/Math.pow(10, l);
    return b[0]>0? b[0]+c:b[0]-c;
}

var s:String = "-952.86";
var i:Number = parseReal(s);
trace(i);
trace(i - -952.86);   //Output: 0

M.


On 3/8/06, Boon Chew <[EMAIL PROTECTED]> wrote:
>
> It might not look like a big deal until you are using Flash to do some
> financial app, where you obtain a number from a string variable, convert it
> to a number, divide it with something and Math.floor it, all the while
> thinking it's the same as taking a number, divide it with something and
> Math.floor it.  (The diff can be as big as 1 because of Math.floor).
>
> - boon
>
> Yotam Laufer <[EMAIL PROTECTED]> wrote: -1.13686837721616e-13 is as close
> to zero as you get matey.... 13 places
> after the decimal...
>
> On 07/03/06, Boon Chew  wrote:
> >
> >
> > How do you usually deal with the loss in precision (a bit oxymoron since
> > floating point can't be exactly represented) when converting a floating
> > point string to a number?
> >
> > var s:String = "952.86";
> > var i:Number = parseFloat(s);
> > trace(i);
> > trace(i - 952.86);   // Not zero!
> >
> >
> > - boon
> >
> >
> >
> >
> > ---------------------------------
> > Yahoo! Mail
> > Bring photos to life! New PhotoMail  makes sharing a breeze.
> > _______________________________________________
> > [email protected]
> > To change your subscription options or search the archive:
> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> >
> > Brought to you by Fig Leaf Software
> > Premier Authorized Adobe Consulting and Training
> > http://www.figleaf.com
> > http://training.figleaf.com
> >
>
>
>
> --
> Yotam Laufer | Flash Developer | mobile +44 (0) 79 205 17 212 | skype:
> ubermutt
> _______________________________________________
> [email protected]
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
>
>
> ---------------------------------
> Yahoo! Mail
> Use Photomail to share photos without annoying attachments.
> _______________________________________________
> [email protected]
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
>
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to