Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 06-07-2005, 04:37 AM   #1
flash_joker
Registered User
 
Join Date: Jun 2005
Posts: 50
Default Format Number Values

I would like to format number values to include the thousand marks. e.g.: 1,001 not 1001. I do have a function that works when you minus values but not add. If somebody could help me with this problem it would be grate.

The following script works fine if minus a value but i need to add to the current value.

Thanks for your time.
ActionScript Code:
a = 5000 b = 1 trace(format(a + b,true)); // The Function function format (val:Number,thousandMark:Boolean){ var intVal = int(val); var tmp =""; if (thousandMark){ // processing thousand marks var vl = String(intVal).length; var tm = Math.floor(vl/3); var tmpNum=intVal; var mk=""; if (tm > 0){ for (i=0;i<=tm;i++){ i>0?mk=",":mk=""; if (tmpNum%1000!=0){ tmp = tmpNum%1000 add mk add tmp; }else{ if (i<tm) tmp = "000" add mk add tmp; } tmpNum = Math.floor(tmpNum/1000); trace("Parsed number: " + tmp); } } } return tmp }
flash_joker is offline   Reply With Quote
Old 06-07-2005, 05:35 AM   #2
MaxFarago
| efficiency is key |
 
MaxFarago's Avatar
 
Join Date: Jun 2005
Location: Santa Fe, NM
Posts: 101
Send a message via AIM to MaxFarago
Default

Man, I reread your post over and over but I just don't know what you mean! I got the part about formatting commas into your numbers. Do you want them to become strings then? I also didn't understand the subtract/add part of your question.

Here's what I would do if i were converting numbers into strings, including the call:

ActionScript Code:
var yourNum:Number = new Number (213141); var numtoString:String = new String (); var numLength:Number = yourNum.toString().length numtoString = ""; if (numLength > 3) {     numtoString = commaCoder(yourNum);     trace(numtoString); } function commaCoder(yourNum):String {     for (i=0; i < numLength; i++)     {         if(i%3 == 0 && i != 0)             numtoString += ",";         numtoString += yourNum.toString().charAt(i);     }         return numtoString; }
MaxFarago is offline   Reply With Quote
Old 10-03-2008, 06:44 PM   #3
devPrez
digital freakazoid
 
Join Date: Oct 2008
Location: SF
Posts: 1
Default

Quote:
Originally Posted by MaxFarago View Post
Man, I reread your post over and over but I just don't know what you mean! I got the part about formatting commas into your numbers. Do you want them to become strings then? I also didn't understand the subtract/add part of your question.

Here's what I would do if i were converting numbers into strings, including the call:

ActionScript Code:
var yourNum:Number = new Number (213141); var numtoString:String = new String (); var numLength:Number = yourNum.toString().length numtoString = ""; if (numLength > 3) {     numtoString = commaCoder(yourNum);     trace(numtoString); } function commaCoder(yourNum):String {     for (i=0; i < numLength; i++)     {         if(i%3 == 0 && i != 0)             numtoString += ",";         numtoString += yourNum.toString().charAt(i);     }         return numtoString; }
Good code but you forgot one thing - you want to count your 3 places from the rear, not from the front. so while this happens to work well with 6,9, or 12 digit numbers, to format for all, such as millions, you must count from the back so i modified one line of code to create the new function:

Code:
function commaCoder(yourNum):String {
	//var yourNum:Number = new Number();
	var numtoString:String = new String();
	var numLength:Number = yourNum.toString().length;
	numtoString = "";


	for (i=0; i<numLength; i++) { 
		if ((numLength-i)%3 == 0 && i != 0) {
			numtoString += ",";
		}
		numtoString += yourNum.toString().charAt(i);
		trace(numtoString);
	}
	return numtoString;
}
All i did was change
Code:
if (i%3 == 0 && i != 0) {
to
Code:
if ((numLength-i)%3 == 0 && i != 0) {
and this works great - thanks!
devPrez is offline   Reply With Quote
Reply


Thread Tools
Display Modes 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 Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 12:06 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.