Oct 13

Thousands Separater Function (addComma)

Formats an integer with commas every three digits

function comma(input)
	if isnull(input) then
		comma = 0
	else
		if len(input) > 3 and len(input) < 7 then
			comma = left(input,(len(input)-3)) & "," & right(input,3)
		elseif len(input) > 6 and len(input) < 10 then
			comma = left(input,(len(input)-6)) & "," & mid(input,(len(input)-5),3) & "," & right(input,3)
		elseif len(input) > 9 and len(input) < 13 then
			comma = left(input,(len(input)-9)) & "," & mid(input,(len(input)-8),3) & "," & mid(input,(len(input)-5),3) & "," & right(input,3)
		elseif len(input) <= 3 then
			comma = input
		end if
	end if
end function

Permanent link to this article: https://www.wperry.net/code/addcomma/

Leave a Reply

Your email address will not be published.