Now Available:

Featured Resource:

line

Newsletter

Email Address:


line

Ask the Expert

Have a question for our resident expert? Email your questions to Greg.

« BSOD Through the Ages | Main | Parse Logs with Log Parser »

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!

TrackBack

TrackBack URL for this entry:
http://www.realtime-windowsserver.com/type/mt-tb.cgi/85

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 :-/

Thanks for posting this. I was just thinking about writing one.

Post a comment

(All comments are approved by site leader before appearing here. Thanks for commenting!)

line

Greg Shields' Bio:

Greg Shields, is an independent author, instructor, and IT consultant based in Denver, Colorado, and a co-founder of Concentrated Technology. With nearly 15 years of experience in information technology, Greg has developed extensive experience in systems administration, engineering, and architecture specializing in Microsoft systems management, remote application, and virtualization technologies. Greg is a Contributing Editor for Redmond Magazine, MCPmag.com, and Virtualization Review Magazine and is the author of five books, including Windows Server 2008:  What’s New / What’s Changed. Greg is also a highly sought-after instructor and speaker, speaking regularly at conferences like TechMentor Events, and producing computer-based training curriculum for CBT Nuggets.  Greg is a recipient of Microsoft "Most Valuable Professional" award with a specialization in Windows Terminal Services.