given string n which is your number and assumming:
loop through the characters in n to find the first occurence of the '.' character. Record it's index in n.
If you dont find it, set the index to be the length of n
starting from the front of n, use the mod 3 of the index of the '.' minus index of the current character to determine if a comma should be inserted behind it. loop through n until you reach the '.' character.