2014-03-02 4 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"; 

참고 : 숫자를 문자열로 변환 할 필요는 없습니다. 그것들을 String으로 연결할 때 암시 적으로 수행됩니다.

관련 문제