将百分比转换为小数

时间:2012-05-18 23:33:26

标签: actionscript-3

单击按钮后,我尝试将百分比转换为小数。 我有一个输入文本字段用于用户输入,另一个用于输入文本字段,即 一个动态文本字段,只显示单击按钮后的结果。

我有两个类:main和我的数学实用程序类。 我的问题是我不知道如何调用该函数 主类中的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。   On var percentResult:String = roundedDecimal;

1 个答案:

答案 0 :(得分:0)

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

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

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

var percentResult:String = String(roundedDecimal);