Is 1 always equivalent to "1"?

Hi folks -

I have tested and noticed that in a form, if I have one field that stores
the text string “1” … and I later ask if that field is equal to 1 (the
number, no quotes) … that it does see them as equivalent.

Can I depend on that always functioning? Because I am going back and
changing a number of forms to make it so that some hidden values will in
fact output the numeral without quotes… But I want to be sure that that’s
not going to cause havoc in logic elsewhere in the app that is checking to
see if those values are equal to quotes “1”. (I’d rather not go
through and have to change all that logic right now at the moment!)

Thanks –

Eric

I wouldn’t rely on automatic casting from string to integer but to use the
number() function. See
https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-number

··· On Thursday 18 December 2014 02:15:44 Eric Stephan wrote: > see if those values are equal to quotes "1". (I'd rather not go > through and have to change all that logic right now at the moment!)


Charles Flèche
mHealth Advisor
Télécoms Sans Frontières http://www.tsfi.org
Première Urgence - Aide Médicale Internationale http://www.pu-ami.org

Really appreciate this information! --Eric

Eric,

I agree with Charles that in general this assumption can be dangerous. I
made a quick test form which covers some of the cases that you may be
interested in, here are the results in case it helps you make a decision

1 = “1”: true
1 < “1”: false
1 <= “1”: true
1 < int(“1”): false
1 + “1”: 2
"1" + 1 = : 2

1 = “1.0”: true
1 < “1.0”: false
1 <= “1.0”: true
1 < int(“1.0”): false
1 + “1.0”: 2
"1.0" + 1 = : 2

1 = “1.3”: false
1 < “1.3”: true
1 <= “1.3”: true
1 < int(“1.3”): false
1 + “1.3”: 2.3
"1.3" + 1 = : 2.3

1 = “f”: false
1 < “f”: false
1 <= “f”: false
1 < int(“f”): false
1 + “f”: NaN
"f" + 1 = : NaN

1 = “”: false
1 < “”: false
1 <= “”: false
1 < int(""): false
1 + “”: NaN
"" + 1 = : NaN

··· On Thu, Dec 18, 2014 at 5:51 AM, Charles Flèche wrote: > > On Thursday 18 December 2014 02:15:44 Eric Stephan wrote: > > see if those values are equal to quotes "1". (I'd rather not go > > through and have to change all that logic right now at the moment!) > > > I wouldn't rely on automatic casting from string to integer but to use the > number() function. See > > https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-number > > -- > Charles Flèche > mHealth Advisor > Télécoms Sans Frontières http://www.tsfi.org > Première Urgence - Aide Médicale Internationale http://www.pu-ami.org > > -- > You received this message because you are subscribed to the Google Groups > "commcare-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to commcare-users+unsubscribe@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. >