Skip to main content
Inspiring
January 17, 2012
Answered

Multiplying numbers with decimal points

  • January 17, 2012
  • 1 reply
  • 579 views

Hi guys,

Probably a simple solution to this one, I have a function that adds the values of an array together and displays the output in a textArea(myText).

The code below works fine, but the output number is "11.5" I need this to be "11.50" and again if we set the array index [1] to "0" instead of ".50" I would need to show "11.00" instead of "11" which is what I am getting now!

var myArray:Array = new Array(1.50,.50,0,9.50);

function addValues(myArray:Array):Number{

var arraySum:Number = 0;

for (var i:uint=0; i< myArray.length; i++){

     arraySum += myArray;

}

return arraySum

}

trace(addValues(myArray));

myText.text = String (addValues(myArray));

Any help would be greatly appreciated!

This topic has been closed for replies.
Correct answer Ned Murphy

Use the toFixed() method of the Number class...

myText.text = String (addValues(myArray).toFixed(2));

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
January 17, 2012

Use the toFixed() method of the Number class...

myText.text = String (addValues(myArray).toFixed(2));

RobulonZAuthor
Inspiring
January 17, 2012

That's the badger!

Cheers Ned!