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.