2014-03-02 44 views
0

我正在使用動作腳本3,出於某種原因,在我的代碼中toFixed()命令不是舍入。我已經公佈了輸出的屏幕截圖。toFixed()不是四捨五入的

這裏是我的代碼

//Makes Button wait for a mouse click if button is click table_print is called 

var round; 
var round2; 
print_table.addEventListener(MouseEvent.CLICK, table_print); //listen for an event if the mouse is clicked. 
function table_print(e:MouseEvent):void{ //table_print function begins 

var Investment : Number = Number(txt_input1.text); //checks user input of Yearly Investment. 
var Interest : Number = Number(txt_input2.text)/100; //checks user input of Interest Rate. 
var Years : Number = Number(txt_input3.text); //checks user input of Number of Years. 
var x : Number = 1; //year number 
var i_amount : Number; //initial amount 
var a_amount : Number; //the amount added with the interest will be seperated to two scenarios 
var a_amountr = a_amount.toFixed(2) // round to two decimal places 
var interest_amount : Number; 
var interest_amount2 : Number; 
var firstturn:Boolean = true; // sets firt turn equal to true 
var total_amount : Number; // total amount in account during that year 
    while (x <= Years){ // execute while less than or equal to user inputed years 

     if (firstturn == false){ 
     label_year.text += String(x) + "\r"; //print out year from 2 - user input 
     a_amount = total_amount + Investment; //set amount equal to previous total + yearly investment 
     label_amount.text += String(a_amount) + "\r"; //print out the amount in account 
     interest_amount2 = a_amount * Interest; // calculates interest of 2 - user inputted year. 
     round2 = interest_amount2.toFixed(2); 
     label_interest.text += String(round) + "r"; //print out 2- user inputted years interest 
     total_amount = interest_amount2 + a_amount; // calculate total amount in account during years 2- user inputed year 
     label_total.text += String(total_amount) + "\r"; //print out total amount in account during years 2- user inputed year 
     } 

     if (firstturn == true){ 
     i_amount = Investment * x; //every single year the investment is added again 
     interest_amount = i_amount * Interest; //when used this calculates interest of first year 
     round = interest_amount.toFixed(2); 
     total_amount = i_amount + interest_amount; // calculates total amount in first turn 
     label_year.text += String(x) + "\r"; //print out the number of years 
     label_amount.text += String(i_amount) + "\r"; //print out the amount in account 
     label_interest.text += String(round) + "\r"; //print out the interest gained from amount 
     label_total.text += String(total_amount) + "\r"; //print out the total money in account during year 
     firstturn = false; //no longer the first turn. 
     } 

     x++; //add an additional year 
    } 
} //function ends 

這是我的輸出

http://gyazo.com/14cf0da1b56931dbfbedcb12a246ef4b.png

回答

0

它看起來像有幾個誤區

首先,計算一個名爲round2變量

round2 = interest_amount2.toFixed(2); 

然後你顯示的一輪價值:

label_interest.text += String(round) + "r"; 

其次,TOTAL_AMOUNT非圓值的總和,而不是四捨五入本身:

total_amount = interest_amount2 + a_amount; 

也許你可以計算出每一個數值,那麼在這個例子中顯示圓角值,比如:

label_amount.text += a_amount.to_fixed() + "\r"; 

注意你不需要投數爲字符串;當它們與一個字符串連接時,它會隱式完成。