2012-05-18 129 views
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: Implicit coercion of a value of type Number to an unrelated type String. On var percentResult:String = roundedDecimal;

+0

看起來你不知道你在這段代碼在做什麼......所有的 –

+0

首先,其餘代碼很好。我遇到的一小部分是在底層和Util類。我得到了它的工作原理,並發現我走在了正確的軌道上,但我需要改變一些事情。我會添加代碼,只是你可以看到我需要做多少改變。感謝Lukasz推動正確的方向。 – Plextor3009

+0

看到它並不那麼需要改變。我以初學者的身份做事,所以這裏的大多數人忘記了他們是如何開始的。這可能看起來像我做錯了,當我真的不是。 – Plextor3009

回答

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

請注意,您的函數返回一個值,所以如果你這樣做,返回值將是分配給textfield。 「String()」將返回的數字轉換爲字符串。

編輯: 啊,在這裏你可以使用鑄造:

var percentResult:String = String(roundedDecimal); 
+0

謝謝,它從Util類中刪除了這個錯誤,但增加了三個新的錯誤:錯誤的參數個數。預期1 訪問未定義的屬性percentResult。 訪問未定義的屬性百分比。 到MathUtil.percentToDecimal() MathUtil.percentToDecimal(percentResult) \t \t \t this.pResult2.text = MathUtil.percentToDecimal(百分比); – Plextor3009

+0

我只需要解決我在主類私有函數btn_EventListener – Plextor3009