Ryan Watts
Ryan Watts

Reputation: 641

Add a number and a Text Input value with adobe flex

I am trying to add a number and a text input value to display in a label. here is my code thus far.

  'lblAnswer.text = bloodglucose + 100;'

Please tell me what I am doing wrong.

Upvotes: 0

Views: 595

Answers (4)

Ryan Watts
Ryan Watts

Reputation: 641

** Update ,

I am changing the code from the update that was previously posted. I initially found that because I was including the string value inside of the equation this is what was prompting an error. You have to wrap the converted components to Number inside of the string all together. Basically convert the components to a number, then convert the answer received into a string.

Below is an example of the wrong code.

txtAnswer = (String(Number(bloodglucose)+100)) / 36)).toFixed(2)

Below this line is the fixed code.

txtAnswer.text = String( (Number(bloodglucose.text) + (Number(100))/ (Number(36))).toFixed(2) ; 

The .toFixed Property signifies how many decimal places I want the returned value to display.

Upvotes: 0

Mike Petty
Mike Petty

Reputation: 949

Sunil is correct - when doing mixed type addition, the UI input first needs to be coerced to either int or Number. IE: Number(bloodglucose) + 100; This assumes bloodglucose is actually a getter to the input text reference. If it's not, then you need to coerce the property and not the id of the component.

Getter: public function get bloodglucose():Number { return Number(myInput.text); }
In method: lblAnswer.text = bloodglucose + 100;

or (bloodglucose is a UIComponent):
In method: lblAnswer.text = Number(bloodglucose.text) + 100;

Upvotes: 2

Varun Bajaj
Varun Bajaj

Reputation: 1043

Please try following answer -

bloodglucose += 100;
lblAnswer.text = String(bloodglucose);

Hope this will work :)

Upvotes: 2

Gligor
Gligor

Reputation: 2940

You should use String(int i)

lblAnswer.text = String(bloodglucose + 100);

Update: What about something like this:

var i:int = bloodglucose + 100;
var s:String = String(i);
lblAnswer.text = s;

Upvotes: 1

Related Questions