Flash Builder将数字转换为字符串

时间:2012-10-05 03:51:10

标签: flex flex3 flash-builder flexbuilder

我正在尝试计算BMI并为数字设置标签,但它会一直返回“NaN”而不是数字。

完整代码:

    <?xml version="1.0" encoding="utf-8"?>
    <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" title="BMI Calculator">

<fx:Script>
    <![CDATA[
        public var weightnum:Number;
        public var heightnum:Number;
        public var resultvar:Number;
        protected function button1_clickHandler():void
        {
            weightnum = Number(weightvar);
            heightnum = Number(heightvar);
            resultvar = weightnum * 4.4 / (heightnum * heightnum);
            resultstr.text = resultvar.toString();
        }
    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label left="30" top="30" text="Your Weight (lbs)"/>
<s:TextInput id="weightvar" restrict="0-9" x="30" y="53" width="80%"/>
<s:Label x="30" y="104" text="Your Height (feet)"/>
<s:TextInput id="heightvar" restrict="0-9" x="30" y="127" width="80%"/>
<s:Button x="30" y="202" label="Calculate" click="button1_clickHandler()"/>
<s:Label id="resultstr" x="30" y="253" text="" />

2 个答案:

答案 0 :(得分:0)

使用number.toString()

而不是使用String()进行强制转换

答案 1 :(得分:0)

您正在尝试将TextInput转换为数字(Number(weightvar);)。 这显然是行不通的。

您需要做的是将TextInput的{​​{1}}属性转换为数字,就像这样:

text