12 Replies Latest reply on Nov 24, 2007 11:55 AM by kglad

# AS 2 - From String to Number conversion Anomaly

There is some strange behavior into Flash subsystem about String --> Number conversion

Execute this simple code.

var strAux:String="1.15";
if(!isNaN(strAux)){
if(Number(strAux)>1.15){
trace("Error. This condition must be false")
}
//There is some sub system problem and I search it
if(Number(strAux)>=1.15000000000000001 && Number(strAux)<=1.15000000000000009){
trace("Range of problem")
}
//Illogical cases
if(Number(strAux)==1.15000000000000003){
trace("Correct digit found case 1")
}
if(Number(strAux)==1.15000000000000004){
trace("Correct digit found case 2")
}
if(Number(strAux)==1.15000000000000005){
trace("Correct digit found case 3")
}
}else{
}
• ###### 1. Re: AS 2 - From String to Number conversion Anomaly
You haven't really explained what the problem is. My guess is that you are running into one well known problem of all computers and the other is a limit of Flash.

Sometimes and for certain values, the binary conversion from a decimal (in base 10) number with decimals (fractional numbers) to a binary (what the computer uses internally) and then back again will produce strange artifacts where a number will come out with a lot of zeros after the decimal and a 1. Or sometimes with a lot of nines. It is just the nature of trying to represent numbers in computer. All systems suffer from it, some use special techniques to overcome it - and some don't.

The other issue I think you are having is that Flash has 15 significant digits (I think that is it. So by having more than 15 places to the right all of those numbers are equal because flash isn't seeing that digit all the way out to the right.

Finally, you aren't converting to a Number you are casting as a number. If you want to convert a string to a number use parseInt or parseFloat.
• ###### 2. Re: AS 2 - From String to Number conversion Anomaly

Yes, the problem is it.
I know this problem in C++.
But AS2 is an authoring language and I think this problems can not appears into it.
I lost few minutes to found it but somebody can lost more hours?!

Regards
• ###### 3. Re: AS 2 - From String to Number conversion Anomaly

I used parseFloat() but problem is the same.
The conditiond are true.

if(parseFloat("1.15")>1.15){
trace(" :O ")
}

var strAux:String="1.15";
if(parseFloat(strAux)>1.15){
trace(" :~ ")
}

Have you a solution? I need precision.
Thanks
• ###### 4. Re: AS 2 - From String to Number conversion Anomaly
Sorry I don't I've never really had to deal with this. What exactly are you trying to do?
• ###### 5. Re: AS 2 - From String to Number conversion Anomaly
flash numbers are accurate to 15 decimal places. you're not going to get any more precision than that when using flash.
• ###### 6. Re: AS 2 - From String to Number conversion Anomaly
hi, this is not a precision problem but of conversion accurance.
• ###### 7. Re: AS 2 - From String to Number conversion Anomaly
it's a precision problem. everything you've posted is related to a precision issue.
• ###### 8. Re: AS 2 - From String to Number conversion Anomaly

Thanks for attention.
I am writing an funcion that compare a lot of decimal number.
the precision of these decimals is two.
I need that problems like:
if(parseFloat("1.15")>1.15){
trace(" :O ")
}

var strAux:String="1.15";
if(parseFloat(strAux)>1.15){
trace(" :~ ")
}

will not happens.
If I will found a solution I will happy to put it on line for contribute.

Regards
• ###### 9. Re: AS 2 - From String to Number conversion Anomaly
Yes you are right, it is related to subsystem that reflects the problem to conversion mechanism.
Have you a solution?
• ###### 10. Re: AS 2 - From String to Number conversion Anomaly
:

• ###### 11. Re: AS 2 - From String to Number conversion Anomaly
ok, it appears to be a working solution.
Thank you
• ###### 12. Re: AS 2 - From String to Number conversion Anomaly
you're welcome.