Formatting Large Numbers

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm
Location: Seattle, USA

Formatting Large Numbers

Post by townsend » Fri Jun 24, 2011 6:56 pm

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

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: Formatting Large Numbers

Post by Dixie » Fri Jun 24, 2011 8:07 pm

Hi...

This road has been trod before.... :D
http://forums.runrev.com/viewtopic.php?f=49&t=6987
be well

Dixie

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm
Location: Seattle, USA

Re: Formatting Large Numbers

Post by townsend » Fri Jun 24, 2011 8:31 pm

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

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm
Location: Seattle, USA

Re: Formatting Large Numbers

Post by townsend » Sun Jun 26, 2011 7:21 pm

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.
Last edited by townsend on Sun Jun 26, 2011 8:37 pm, edited 1 time in total.

worcestersource
Posts: 24
Joined: Wed Mar 17, 2010 3:51 pm
Location: Worcester, UK
Contact:

Re: Formatting Large Numbers

Post by worcestersource » Sun Jun 26, 2011 7:34 pm

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

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm
Location: Seattle, USA

Re: Formatting Large Numbers

Post by townsend » Sun Jun 26, 2011 8:46 pm

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()

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”