Hi,
Can anyone tell me if there is an API function or a MASM32 function that will take a number and insert the commas into it.

I would like to take this number,

1234567

and be able to display it like this,

1,234,567

Thanks,
bgong68
Posted on 2003-11-04 10:16:37 by bgong68
Well, if you're just looking for something quick and dirty, you might try this... there are alot of better ways to do it though, I just didn't put my mind to it. pbuffer must contain the address to a null terminated string that is large enough to contain the string with commas.
InsertCommas proc uses edi esi Number:UINT,pbuffer:DWORD

LOCAL szNumber[32] :BYTE
LOCAL buffer[32] :BYTE
LOCAL comma :DWORD

invoke dwtoa,Number,ADDR szNumber

mov comma,","

invoke lstrlen,ADDR szNumber
xor edx,edx
mov ecx,3
div ecx
mov edi,eax
lea esi,szNumber
add esi,edx
push edx
inc edx ; null terminator
invoke lstrcpyn,pbuffer,ADDR szNumber,edx
pop edx
test edi,edi
jz done
test edx,edx
jz @F
.REPEAT
invoke lstrcat,pbuffer,ADDR comma
@@:
invoke lstrcpyn,ADDR buffer,esi,4
invoke lstrcat,pbuffer,ADDR buffer
add esi,3
dec edi
.UNTIL edi == 0

done:
ret

InsertCommas endp


EDIT : Made a small correction when the number of digits is divisible by 3
Posted on 2003-11-04 11:15:19 by donkey
Hi donkey,
THANKS! This is perfect for what I need. I also want to let people know that I found another good code example at

http://www.madwizard.org/snippets/viewSnippet.php?s_ID=28

Thanks,
bgong68
Posted on 2003-11-04 12:06:42 by bgong68