PDA

View Full Version : Rounding to two Decimal Places



ASjess
June 26th, 2009, 04:10 PM
I haven't used actionscript 3.0 in a while and I'm finding myself slightly lost.
I have a fully functional calculator but I need it to return numbers with a max of two decimal places. (x.00)

I'm not sure where to input the code and how to make this work.

Please help!

here's my code:


total_btn.addEventListener(MouseEvent.CLICK, getTotals);
function getTotals(event:MouseEvent):void
{
var annualMiles:Number = Number(annualmiles_txt.text);
var scrMpg:Number = Number(scrmpg_txt.text);
var scrAdvantage:Number = Number(scradvantage_txt.text)/100;
var doseRate:Number = Number(doserate_txt.text)/100;
var fuelCost:Number = Number(fuelcost_txt.text);
var defCost:Number = Number(defcost_txt.text);
//this is for the scr output
var scrmpgoutput:Number = scrMpg;
scrmpgoutput_txt.text = String(scrmpgoutput);
//this is for the egr ouput
var egrmpg:Number = scrMpg - (scrMpg * scrAdvantage);
egrmpg_txt.text = String(egrmpg);
//this is for the fuel consumed scr output
var fuelconsumedscr:Number = annualMiles / scrMpg;
fuelconsumedscr_txt.text = String(fuelconsumedscr);
//this is for the fuel consumed egr ouput
var fuelconsumedegr:Number = annualMiles / egrmpg;
fuelconsumedegr_txt.text = String(fuelconsumedegr);
//this is for the def required scr
var defrequiredscr:Number = fuelconsumedegr * doseRate;
defrequiredscr_txt.text = String(defrequiredscr);
//this is for the def required egr
var defrequiredegr:Number = annualMiles * 0;
defrequiredegr_txt.text = String(defrequiredegr);
//this is for the def/fuel cost combined scr
var scrfuelcost:Number = (fuelconsumedscr * fuelCost) + (defrequiredscr * defCost);
scrfuelcost_txt.text = String(scrfuelcost);
//this is for the def/fuel cost combined egr
var egrfuelcost:Number = fuelconsumedegr * fuelCost;
egrfuelcost_txt.text = String(egrfuelcost);
//this is for the difference between the two fuel costs
var difference:Number = scrfuelcost - egrfuelcost;
difference_txt.text = String(difference);

}



the only things that need to be rounded are the return values...
Thanks SO Much to anyone who can help!!

creatify
June 26th, 2009, 04:32 PM
you can use number.toFixed() - see here (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Number.html#toFixed())

blanked
June 26th, 2009, 04:37 PM
Math.round(variable(*100))/100;

This rounds to the nearest hundredth of the decimal.