String to Int in qml
-
Hi All,
Regarding textInputs.
for example:
i got 2 TextInputs.@
TextInput{ id: textInput1; text:"1"}
TextInput{id:textInput2; text: textInput1.text + 1} // i'm expecting an output "2" in textInput2 but now the result is "11"
@So how to convert string to int ?
Thanks in advance. -
In your code string + int evaluates to string as per javascript expressions. Use parseInt for self explainable code.
-
@blam's suggestion is a good one but CAUTION! If you're using parseInt please always always use the radix parameter (as described "here":http://www.w3schools.com/jsref/jsref_parseInt.asp).
parseInt("11") will always result in 11, however parseInt("011"), because the string starts with 0, it will be interpreted as OCTAL, with a result of 9! Hard to track down bugs ahoy. parseInt("011", 10) will give you the result you expect!