Question

I am using action script 3 and for some reason in my code the toFixed() command is not rounding. I have disclosed a screen shot of the output.

Here is my code

//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

This is my output

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

No correct solution

OTHER TIPS

It looks like there are several errors

First you calculate a variable named round2

round2 = interest_amount2.toFixed(2);

and then you display the value of round:

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

Second, total_amount is the sum of non rounded values, and is not rounded itself:

total_amount = interest_amount2 + a_amount;

Maybe you could calculate every numeric values, then display rounded values, like in this example:

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

Note you don't need to cast Numbers to Strings; it is done implicitly when you concatenate them with a String.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top