Thousands Separators
Peter W A Wood
peterwawood at gmail.com
Mon Jul 27 23:07:53 EDT 2015
Mike
> On 27 Jul 2015, at 19:14, Michael Doub <mikedoub at gmail.com> wrote:
>
> Peter, there is a function in the MasterLibrary that does this for you. Look in the Text section.
>
> https://www.dropbox.com/s/3wpwn3hfbmpl7sk/MasterLibrary.livecode?dl=0
>
> Regards,
> Mike
The MasterLibrary is impressive, I should have looked at it before.
The function didn’t seem to have the exact format that I needed but it was a good stimulus to write one that meets my needs. I wanted to format the numbers in what I call accounting format - thousands separators with negative numbers in brackets (or parentheses depending from where one hails).
Thanks to LiveCode’s chunking the function is very short, dealing with the arguments takes almost as much code as the function. I include it here in case it is of use to others. I’m sure it could be improved and would welcome any suggestions.
function numToAccounting pNum, pDecimalSeparator, pThousandSeparator
# Purpose: Converts a number to accounting format
# Inputs:
# pNum The number to be converted
# pDecimalSeparator The character to be used, default "."
# pThousandSeparator The character to be used, default ","
# Returns: The number in accounting format rounded toplevel
# decimal places
local tCount
local tDecimalSeparator
local tNum
local tStart
local tThousandSeparator
# set the separators
if pThousandSeparator is empty then
put "," into tThousandSeparator
else
put pThousandSeparator into tThousandSeparator
end if
if pDecimalSeparator is empty then
put "." into tDecimalSeparator
else
put pDecimalSeparator into tDecimalSeparator
end if
#format the numbers
put format("%.2f", pNum) into tNum
put tDecimalSeparator into char -3 of tNum
put 0 into tCount
put the length of tNum - 3 into tStart
repeat with i = tStart down to 1
add 1 to tCount
if tCount = 4 then
if char i of tNum = "-" then exit repeat
put tThousandSeparator after char i of tNum
put 1 into tCount
end if
end repeat
if pNum < 0 then
put "(" into character 1 of tNum
put ")" after tNum
end if
return tNum
end numToAccounting
More information about the use-livecode
mailing list