Skip to main content
Inspiring
January 26, 2012
Answered

round to two decimal places

  • January 26, 2012
  • 1 reply
  • 881 views

my cost per month textbox typically returns multiple decimal places, is it possible to limit it's output to two decimal places like real monetary calculations?

calculatebutton.addEventListener(MouseEvent.CLICK, doMyCalculation);

calculatebutton2.addEventListener(MouseEvent.CLICK, doMyCalculation2);

costpermonth.restrict = ".0-9";

{

    var myAnswer : Number;

    myAnswer = Number(costofitem.text) - Number(amountsaved.text);

    amountneedtosave.text = String(myAnswer);

}

function doMyCalculation2(e:MouseEvent):void

{

    var myAnswer2 : Number;

    myAnswer2 = Number(amountneedtosave.text) / Number(howmanymonths.text);

    costpermonth.text = String(myAnswer2);

}

This topic has been closed for replies.
Correct answer kglad

use the toFixed() method of numbers:

amountneedtosave.text = myAnswer.toFixed(2);

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
January 26, 2012

use the toFixed() method of numbers:

amountneedtosave.text = myAnswer.toFixed(2);