toFixed()不会舍入

时间:2014-03-02 16:33:14

标签: 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 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

1 个答案:

答案 0 :(得分:0)

看起来有几个错误

首先计算名为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";

请注意,您不需要将数字转换为字符串;当你用String连接它时,它是隐式完成的。