View Full Version : Format Int to String Function?
Qugh
October 20th, 2005, 10:40 AM
Anyone have a good idea how I should format a long integer into string format with commas? I don't know if there's a built-in func for this, but I haven't found it.
raw int: 2000000
parsed string: 2,000,000
e.s.x.s
October 20th, 2005, 11:09 AM
u should use. Number.toString() .. than String.substr() for parsing ;) :mu:
Barn
October 20th, 2005, 12:46 PM
Also might see example file Currency Formatting (http://www.canfieldstudios.com/flashmx/currfrmt)
Qugh
October 21st, 2005, 07:50 AM
Thanks a lot for the input! I ended up writing a custom function to do the task properly. Here it is (in case anyone else needs to use it):
//function to parse dollars to even numbers
function formatDollar(pVar)
{
var parsedString = "";
//find where to start adding the comma (out of every 3)
skipChars = String(pVar).length % 3;
if( skipChars==0 ){ skipChars = 3; }
//split into array
strArray = String(pVar).split("");
//how soon to add comma
var recurKey = skipChars;
//loop through string to add comma
for( i=0; i<strArray.length; i++ )
{
parsedString = parsedString + strArray[i]
//determine if comma is inputted
recurKey--;
if( recurKey < 1 && i<strArray.length-1 )
{
parsedString = parsedString + ",";
recurKey = 3;
};
}
return "$ " + parsedString;
}
JimmyH
October 21st, 2005, 08:02 AM
Actionscript 3.0's regular expressions will be handy for this sort of task, should reduce your function right down.
xcaliber
October 28th, 2005, 04:29 PM
Thanks a lot for the input! I ended up writing a custom function to do the task properly. Here it is (in case anyone else needs to use it):
Hi Qugh,
I have a quick question for you on the code posted for currency formatting.
I am tring to get a set of dynamic txt boxes to show the final calculations in the currency format. When I changed the "var parsedString" to the var "" that I am using, nothing happens. Can you point me in the right direction on what I am doing wrong.
Thanks,
xcaliber
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.