Comma Format

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
lohill
Posts: 770
Joined: Tue Dec 08, 2009 6:37 pm

Comma Format

Post by lohill » Tue Sep 18, 2012 9:25 pm

Is there a function for taking an integer and formatting it so that it contains its commas in the appropriate places? I have been reading about 'format' but can't make any sense of it.

Thanks,
Larry

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: Comma Format

Post by dunbarx » Tue Sep 18, 2012 10:57 pm

Hi.

You are talking about a format that would transform: 1234567 to 1,234,567?

I think you have to roll your own. But this is a fun project, and very simple. Do you need help? Write back if you do.

Craig Newman

lohill
Posts: 770
Joined: Tue Dec 08, 2009 6:37 pm

Re: Comma Format

Post by lohill » Wed Sep 19, 2012 12:15 am

Hi Craig,

This is my idea of fun too. I did end up rolling my own which now looks like this:

Code: Select all

function GetCommaFormat tWholePart
   put value(tWholePart) into tWholePart
   if tWholePart < 0 then
      put true into tNegative
      put abs(tWholePart) into tWholePart
   end if
   put empty into tFormattedWhole
   repeat until tWholePart < 1000
      put comma & char -3 to -1 of ("000" & tWholePart mod 1000) before tFormattedWhole
      put tWholePart div 1000 into tWholePart
   end repeat
   if tNegative is true then
      put "-" before tWholePart
   end if
   put tWholePart  before tFormattedWhole
   return tFormattedWhole
end GetCommaFormat
In the beginning my function was lacking the line ' put value(tWholePart) into tWholePart' and when 6000 was passed to it from my USCurrency function, the getCommaFormat function was returning 5000 to it giving me a result of $5,000.00. If I used the message box and wrote 'put getCommaFormat(6000)' into it the correct answer got returned. My original function ( without value(tWholePart) in conjunction with USCurrency() appeared to work for everything except with even thousands. With the addition of that line it appears to work for everything.

Thanks,
Larry

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Comma Format

Post by dave_probertGA6e24 » Wed Sep 19, 2012 2:42 am

Hi Larry,

Just took a look at your function - nice, but there is a problem if you try to format a decimal number.

If you do e.g.: GetCommaFormat(123456789.98) the result is "123,456,.98"
or GetCommaFormat(12345.98) the result is "12,.98"

It drops the last 3 number before the decimal point.

I'm just on my way out so I can't have a play at fixing the code. I'm sure you will be much faster at it anyway :)

Just thought I'd give you a heads-up,

Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: Comma Format

Post by dunbarx » Wed Sep 19, 2012 3:05 am

Well done. I stopped work to play around with this, and so I am grateful.

With a number in a field "f1":

Code: Select all

on mouseUp
   get fld "f1"
   put 0 into charCount
   if  offset(".",it) <> 0 then put  offset(".",it) - 1 into startChar
   else put the number of chars of it into startChar
   repeat with y = startChar down to 1
      add 1 to charCount
      if charCount mod 3 =0 then put "," before char y of  it
   end repeat
   if char 1 of  it = "," then delete char 1 of it
   if char 1 to 2 of it = "-," then delete char 2 of it
   answer it
end mouseUp
Parts of this seem clunky, no? Your turn.

Craig Newman

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Comma Format

Post by sturgis » Wed Sep 19, 2012 3:49 am

Something like this one might work ok.

Code: Select all

function addCommas pNum
   set the itemdelimiter to "."
   if the number of items in pNum > 1 then  put item 2 of pNum into tDecimal -- store the decimal part if it exists
   put item 1 of pNum + 0 into tMain -- did the plus zero to whomp any leading or trailing zeros in the number
      repeat with i = (the number of chars in tMain - 3) down to 3 step -3
         put comma after char i of tMain
      end repeat
   return tMain & "." & tDecimal
end addCommas

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: Comma Format

Post by dunbarx » Wed Sep 19, 2012 4:30 am

Sturgis.

Yep. But you have to lose an errant "." if the argument has none to begin with. Shows you the perils of writing code while driving.

Craig

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Comma Format

Post by sturgis » Wed Sep 19, 2012 4:37 am

Doh, yep. Good catch there. Adjusted code follows because I can't sleep.

Code: Select all

function addCommas pNum
   local tDecimal
   set the itemdelimiter to "."
   if the number of items in pNum > 1 then put "." &  item 2 of pNum into tDecimal 
   put item 1 of pNum + 0 into tMain -- did the plus zero to whomp any leading or trailing zeros in the number
   repeat with i = (the number of chars in tMain - 3) down to 3 step -3
      put comma after char i of tMain
   end repeat
   return tMain & tDecimal
end addCommas

lohill
Posts: 770
Joined: Tue Dec 08, 2009 6:37 pm

Re: Comma Format

Post by lohill » Wed Sep 19, 2012 10:38 pm

Dave,
Just took a look at your function - nice, but there is a problem if you try to format a decimal number.
The problem as originally stated was only to be able to comma format a positive or negative integer. My USCurrency() function isolated the whole number part prior to calling the getCommaFormat() function for just the whole number.

Sturgis,
Thank you for your suggestion. Seeing the simple way in which you isolated the whole number part from the decimal part got me to change my USCurrency() function by encorporating the comma formatting in the function itself. I end up with something which is much more concise from what i had before. Here is mt function as it now stands:

Code: Select all

function USCurrency tNumber, tPlaces
   if tPlaces is empty or tPlaces < 2 then
      put 2 into tPlaces
   end if
   if not isNumber(tNumber) then
      return "Error"
   end if
   If tNumber < 0 then
      put abs(tNumber) into tNumber
      put true into tNegative
   end if
   put round(tNumber,tPlaces) into tNumber
   set the itemdelimiter to "."
   put the number of items of tNumber into tItems
   if tItems = 1 then
      put ".0" after tNumber
   end if
   put item 1 of tNumber into tMain
   repeat with i = (the number of chars in tMain - 3) down to 3 step -3
      put comma after char i of tMain
   end repeat
   put item 2 of tNumber into tDecimal
   repeat until the number of chars of tDecimal = tPlaces
      put tDecimal & "0" into tDecimal
   end repeat
   if tNegative = true then put "-" before tMain
   return  "$" & tMain & "." & tDecimal
end USCurrency
It guarantees for at least two decimal digits but allows for more if desired. I think my only question now is which would be the appropriate way to express a negative dollar amount: $-34.75 or -$34.75. My function provides for the former.

Larry

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

Re: Comma Format

Post by dunbarx » Thu Sep 20, 2012 3:20 pm

Hi.

I think neither form is typical, though the second seems more natural. The way it is usually done is: ($5.00), the parentheses indicating a debit rather than an asset.

Craig Newman

lohill
Posts: 770
Joined: Tue Dec 08, 2009 6:37 pm

Re: Comma Format

Post by lohill » Thu Sep 20, 2012 9:39 pm

Thanks Craig,

I guess that is why I had a hard time deciding which choice looked best - neither one was. The parentheses were a quick fix and look very professional.

Larry

Post Reply