VBScript function to add commas to a large number
I wasted about 2.5 hours today trying to figure out a way to take a large number as input and add in the necessary commas to make it useful. For example, if in my script I query for available disk space, I'll get a response that looks like this:
59305963068
What I really want is a response that looks like this:
59,305,963,068
So I created the function below to add in the commas. I can't tell if this is an elegant solution, but I figured I'd share it with you since I can't seem to find a similar one anywhere in Google. Enjoy!
Function addCommas(strNumber)
If Len(strNumber) > 3 Then
For i = 3 To Len(strNumber) Step 3
strPreString = Left(Right(strNumber, i), 3) & "," & strPreString
Next
If Len(strNumber) Mod 3 > 0 Then
addCommas = Left(strNumber, Len(strNumber) Mod 3) & "," _
& Left(strPreString, Len(strPreString) - 1)
Else
addCommas = Left(strPreString, Len(strPreString) - 1)
End If
Else
addCommas = strNumber
End If
End Function
...and if you've got a better way of doing this, I'd love to hear about it!

Email This!
Digg it!
Del.icio.us
Reddit!
Newsvine
Comments
Saluti..
voi compartecipe interessare posto :)
Io alberini della sega dove uomo volonta desideri a ritrovamento corso como milano .
Corso web e formative di. A richiest
a offre. Un dei. Corso del. Regione corsi..
Mmm.. Noi non poteva raccolta utile le informazioni come corsi on line.
Ammetta quello cercato alcuni :-/
Posted by: dincincate | August 23, 2007 9:10 AM
Thanks for posting this. I was just thinking about writing one.
Posted by: John Trier | August 29, 2007 4:57 PM