简体   繁体   中英

Convert percentage to decimal

I'm trying to convert a percent to a decimal once a button is clicked. I have an input text field for user input and another under it, which is a dynamic text field to just show the result after the button is clicked.

I have two classes: main and my utility class for the math. My problem is I don't know how to call the function from the util class in the main class to get this working.

I can't get this last part working. here is my code:

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;
    }
}

I'm also getting this error:

1067: Implicit coercion of a value of type Number to an unrelated type String. On var percentResult:String = roundedDecimal;

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

Note that your function returns a value, so if you will do this, the returned value will be assigned to textfield. "String()" is casting a returned number to string.

Edit: Ah, and here you can use casting:

var percentResult:String = String(roundedDecimal);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM