Quote:
Originally Posted by MaxFarago
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!