Page 1 of 1
Formatting Large Numbers
Posted: Fri Jun 24, 2011 6:56 pm
by townsend
The Dictionary examples for the Format function aren't very clear.
And I didn't find any Topics or Lessons on this.
I'd like to get some commas inserted into a large number.
For example: 123456789 displayed as 123,456,789
This code doesn't work:
Code: Select all
put format ("###,###", 123456)
--nor this code
set the numberformat to "###,###"
put 123456
Re: Formatting Large Numbers
Posted: Fri Jun 24, 2011 8:07 pm
by Dixie
Hi...
This road has been trod before....
http://forums.runrev.com/viewtopic.php?f=49&t=6987
be well
Dixie
Re: Formatting Large Numbers
Posted: Fri Jun 24, 2011 8:31 pm
by townsend
Thanks Dixie!
Here's the code. I changed the name. I added a check for a string that had been previously formatted. In such a case, the previously formatted string is returned. Also I changed the reponse on a three digit submission to an return of the original number rather then quick exit.
Code: Select all
function with.commas theString
if comma is in theString then
return theString
end if
put the number of chars of theString into charCount
if charCount < 3 then return theString
put 0 into commaCount
repeat with count = charCount down to 1
if commaCount = 3 then
put comma before commaNumbers
put 0 into commaCount
end if
put char count of theString before commaNumbers
add 1 to commaCount
end repeat
return commaNumbers
end with.commas
Re: Formatting Large Numbers
Posted: Sun Jun 26, 2011 7:21 pm
by townsend
The previous code got all messed up if you sent it a number with a decimal point.
So I've updated the code to accommodate decimal points.
Basically I extract the decimal point portion in the beginning, then add it back on afterwards.
This code contains lots of good chunk (string manipulation) examples.
Code: Select all
function with.commas theString
if comma is in theString then return theString
if number of chars of theString = 0 then return theString
put the number of chars of theString into charCount
if "." is in theString then
put offset(".",theString) into dot
put char 1 to dot -1 of theString into body
put char dot to charCount of theString into dot
else if charCount < 3 then
return theString
else
put theString into body
end if
put 0 into commaCount
put the number of chars of body into charCount
repeat with count = charCount down to 1
if commaCount = 3 then
put comma before commaNumbers
put 0 into commaCount
end if
put char count of body before commaNumbers
add 1 to commaCount
end repeat
return commaNumbers & dot
end with.commas
This is a good Handler to save for future use.
Re: Formatting Large Numbers
Posted: Sun Jun 26, 2011 7:34 pm
by worcestersource
Hi Townsend,
If you pop onto RevOnline/User Samples and search for "accounting", you'll find my accountancyFormat() function that will format the large numbers for you in a nice and neat fashion.
Cheers,
Steve
Re: Formatting Large Numbers
Posted: Sun Jun 26, 2011 8:46 pm
by townsend
Thanks Steve! Looks like it does lots of other stuff too.
Formats a number according to UK accounting conventions Useful little function that takes a number and formats it according UK accounting conventions i.e. put brackets around a negative number, adds comma separators etc.
You can also add a leading currency character and change the default characters for the thousands and decimal separators.
Each parameter other than the actual number itself has a default value, making it nice and compact in your own code.
Download here:
function: accountancyFormat()