Page 1 of 1
Comma Format
Posted: Tue Sep 18, 2012 9:25 pm
by lohill
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
Re: Comma Format
Posted: Tue Sep 18, 2012 10:57 pm
by dunbarx
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
Re: Comma Format
Posted: Wed Sep 19, 2012 12:15 am
by lohill
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
Re: Comma Format
Posted: Wed Sep 19, 2012 2:42 am
by dave_probertGA6e24
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
Re: Comma Format
Posted: Wed Sep 19, 2012 3:05 am
by dunbarx
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
Re: Comma Format
Posted: Wed Sep 19, 2012 3:49 am
by sturgis
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
Re: Comma Format
Posted: Wed Sep 19, 2012 4:30 am
by dunbarx
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
Re: Comma Format
Posted: Wed Sep 19, 2012 4:37 am
by sturgis
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
Re: Comma Format
Posted: Wed Sep 19, 2012 10:38 pm
by lohill
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
Re: Comma Format
Posted: Thu Sep 20, 2012 3:20 pm
by dunbarx
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
Re: Comma Format
Posted: Thu Sep 20, 2012 9:39 pm
by lohill
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