Go Back   CodingForums.com > :: Client side development > Flash & ActionScript > Adobe Flex

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-14-2009, 03:38 PM   PM User | #1
loki421
Regular Coder

 
Join Date: Feb 2009
Location: Worcester
Posts: 172
Thanks: 13
Thanked 6 Times in 6 Posts
loki421 is an unknown quantity at this point
Setting Number to 2 decimal places?

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:

Code:
private function getMath(event:Event):void
	{
		var myNumber:Number = new Number(input.text);
				
		var answer:Number = ((myNumber  /100) * 15) + (myNumber);
		answer.toFixed(2);
		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!
loki421 is offline   Reply With Quote
Old 06-14-2009, 06:53 PM   PM User | #2
loki421
Regular Coder

 
Join Date: Feb 2009
Location: Worcester
Posts: 172
Thanks: 13
Thanked 6 Times in 6 Posts
loki421 is an unknown quantity at this point
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?
loki421 is offline   Reply With Quote
Old 06-16-2009, 06:38 PM   PM User | #3
loki421
Regular Coder

 
Join Date: Feb 2009
Location: Worcester
Posts: 172
Thanks: 13
Thanked 6 Times in 6 Posts
loki421 is an unknown quantity at this point
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
loki421 is offline   Reply With Quote
Old 06-16-2009, 09:18 PM   PM User | #4
loki421
Regular Coder

 
Join Date: Feb 2009
Location: Worcester
Posts: 172
Thanks: 13
Thanked 6 Times in 6 Posts
loki421 is an unknown quantity at this point
Ha! Cracked it! For anyone else who's struggling withthis sort of thing, here's the answer:

Code:
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
loki421 is offline   Reply With Quote
Old 06-17-2009, 08:51 PM   PM User | #5
jerry62704
Senior Coder

 
jerry62704's Avatar
 
Join Date: Oct 2007
Location: Springfield, IL
Posts: 1,046
Thanks: 9
Thanked 81 Times in 81 Posts
jerry62704 is on a distinguished road
I've had that problem as well, but chose to use one of the controls:

Code:
  
<mx:CurrencyFormatter id="Price" 
	precision="0"
	rounding="none"
	decimalSeparatorTo="."
	thousandsSeparatorTo=","
	useThousandsSeparator="true"
	useNegativeSign="true"
	currencySymbol="$"
	alignSymbol="left"/>
Then I use that like this:

Code:
public function assignOutputVariables():void {
	// above columns - final results
	var tempFormat:Number;
	tempFormat = FSP.getFinalResults();
	resultPotentialResultsData.data = Price.format(tempFormat);
	assignFinalResultText();
...
}
Just an idea.
__________________
.
.
...and gladly would he learn and gladly teach

Visit www.LiberalsWin.com for humor and the unique Bush/Obama Approval Polls
jerry62704 is offline   Reply With Quote
Old 06-17-2009, 09:28 PM   PM User | #6
loki421
Regular Coder

 
Join Date: Feb 2009
Location: Worcester
Posts: 172
Thanks: 13
Thanked 6 Times in 6 Posts
loki421 is an unknown quantity at this point
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

Thanks though
loki421 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:15 AM.


Advertisement
Log in to turn off these ads.