Flash toFixed()不是四舍五入
我使用的是动作脚本3,由于某种原因,在我的代码中toFixed命令没有舍入。我已经透露了输出的屏幕截图 这是我的密码Flash toFixed()不是四舍五入,flash,fixed,rounding,rounding-error,Flash,Fixed,Rounding,Rounding Error,我使用的是动作脚本3,由于某种原因,在我的代码中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 th
//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
这是我的输出
看起来有几个错误 首先计算一个名为round2的变量
round2 = interest_amount2.toFixed(2);
然后显示round的值:
label_interest.text += String(round) + "r";
第二,total_amount是非舍入值的总和,并且本身不舍入:
total_amount = interest_amount2 + a_amount;
也许您可以计算每个数值,然后显示舍入值,如本例所示:
label_amount.text += a_amount.to_fixed() + "\r";
注意,您不需要将数字强制转换为字符串;当您用字符串连接它们时,它是隐式完成的