View Full Version : Setting Number to 2 decimal places?

06-14-2009, 04:38 PM
Hi all,

I'm trying to get an answer to 2 decimal places, i've got a number value that is returning too many decimals.

Here's the code:

private function getMath(event:Event):void
var myNumber:Number = new Number(input.text);

var answer:Number = ((myNumber /100) * 15) + (myNumber);
var text:String = answer.toString();
answerLabel.text = text;

I'm using toFixed(2) but it's not working, does anyone know where this is going wrong? Any pointers/suggestions would be gratefully appreciated :)

Many thanks in advance all! :D

06-14-2009, 07:53 PM
ok, i've realised that if i use a whole number as the input, then it works fine, but most likely the input will include 2 decimal places, is there a function that will allow the 'answer' to stay at 2 decimals event if the input contains decimals? :confused:

06-16-2009, 07:38 PM
Can no one help wit this then? I've tried all sorts, Math.round, toPersicion, toFixed, but no matter what i use, if i input a decimal the function fails, i've tried looking for a report of a bug in Flex 3 for this, but no one else seems to be having this trouble....

Any suggestions?

Thanks all :D

06-16-2009, 10:18 PM
Ha! Cracked it! For anyone else who's struggling withthis sort of thing, here's the answer:

private function CalculatePercentage(event:Event):void
var myNumber:Number = new Number(price_input.text);

var wholeNumber:Number = (myNumber * 100);

var revertBack:Number = (wholeNumber /10000);

var GetPercentage:Number = (revertBack * 15);

var percentageAnswer:Number = (GetPercentage + myNumber);

var answer:Number = percentageAnswer;

var display:String = answer.toFixed(2);// fixing to 2 decimals
var text:String = display.toString();
withPercentageprice.text = text;

Anyway, hope this helps someone :D

06-17-2009, 09:51 PM
I've had that problem as well, but chose to use one of the controls:

<mx:CurrencyFormatter id="Price"

Then I use that like this:

public function assignOutputVariables():void {
// above columns - final results
var tempFormat:Number;
tempFormat = FSP.getFinalResults();
resultPotentialResultsData.data = Price.format(tempFormat);

Just an idea.

06-17-2009, 10:28 PM
Yeah i did try that one, but for some reason when it gave me the 'answer' it included 2 decimals! ie: 245.674345.87545, wierd hey?

I assumed that it must have been because i was inputting decimals, and therefore outputting them too???

But with the code i posted i can input with decimals and still get only 2 decimal places returned, whatever you prefer i suppose :p

Thanks though :D