0

单击按钮后,我正在尝试将百分比转换为小数。我有一个用于用户输入的输入文本字段,另一个在它下面,这是一个动态文本字段,仅在单击按钮后显示结果。

我有两门课:主课和我的数学实用课。我的问题是我不知道如何从主类中的 util 类调用该函数以使其正常工作。

我无法让这最后一部分工作。这是我的代码:

public class Main extends Sprite
{
    private var pResult:TextField;
    private var pResult2:TextField;

    public function Main()
    {
        super();

        // calling all the functions below

        // adding my graphics to the display
        var baseDBase = new PDBase();
        this.addChild(base);
        base.x = -70;
        base.y = 30;

        //changing the font format
        var format:TextFormat = new TextFormat();//adding the object
        format.size = 14;//font sizing
        format.align = TextFormatAlign.LEFT;//font align

        //result text field
        pResult = new TextField();//adding the object
        this.addChild(pResult);//displaying the object
        pResult.border = true;//setting the border
        pResult.borderColor = 0x30FF00;//setting border color
        pResult.textColor = 0x000000;//setting the font color
        pResult.x = 28;//position left or right
        pResult.y = 70;//position up or down
        pResult.width = 142;// changing width
        pResult.height = 20;// changing height
        pResult.type = TextFieldType.INPUT;//making sure the text area is for input
        pResult.defaultTextFormat = format;//sets text format to defult
        pResult.maxChars = 32;//text limit of characters
        pResult.restrict = "0-9.";//fonts used only

        pResult2 = new TextField();//adding the object
        this.addChild(pResult2);//displaying the object
        pResult2.border = true;//setting the border
        pResult2.borderColor = 0x30FF00;//setting border color
        pResult2.textColor = 0x000000;//setting the font color
        pResult2.x = 28;//position left or right
        pResult2.y = 96;//position up or down
        pResult2.width = 142;// changing width
        pResult2.height = 20;// changing height
        pResult2.type = TextFieldType.DYNAMIC;//making sure the text area is for input
        pResult2.defaultTextFormat = format;//sets text format to defult
        pResult2.maxChars = 32;//text limit of characters
        pResult2.restrict = "0-9.";//fonts used only

        var button:Button = new Button("Calculate");
        this.addChild(button);
        button.x = 10;
        button.y = 130;
        button.addEventListener(MouseEvent.CLICK, btn_EventListener);
    }

    private function btn_EventListener(e:MouseEvent):void
    {
        MathUtil.percentToDecimal(percent)
        this.pResult2.text = percent;
    }
}

public class MathUtil
{

    public function MathUtil()
    {
    }

    public static function percentToDecimal(percentValue:Number):Number
    {
        var percent:Number = percentValue * 100;
        var roundedDecimal:Number = Math.round(percent);
        var percentResult:String = roundedDecimal;
        return percentResult;
    }
}

我也收到此错误:

1067:将 Number 类型的值隐式强制转换为不相关的 String 类型。在 var percentResult:String = roundedDecimal;

4

1 回答 1

0
this.pResult2.text = String(MathUtil.percentToDecimal(percent));

请注意,您的函数返回一个值,因此如果您这样做,返回的值将分配给文本字段。"String()" 将返回的数字转换为字符串。

编辑:啊,在这里你可以使用铸造:

var percentResult:String = String(roundedDecimal);
于 2012-05-18T23:53:53.483 回答