Formatting Integers without System.Globalization in Visual Studio

You’ve probably felt the need to show data results with massive numbers to a general audience – but because there is no digit separation, they lose track of which quadrillion they were up to – correct?

 

In Visual Studio there are regex and globalization (en_US) ways of formatting strings or integers to add comma separation (e.g. 5601928.2 = 5,601,928.2), but these don’t always work.

 

The following snippet allows you to iterate over a Double or Int64 to add comma’s in string format (not limited to overflow like int32!)

 

Dim i = ""
Dim j = ""
Dim k = ""
Dim intTemp As Int64 = Convert.ToInt64(yourInteger)
Dim intTempLen As Integer = intTemp.ToString.Length
Do While intTempLen > 3
i = intTemp.ToString.Substring(0, intTempLen - 3)
j = intTemp.ToString.Substring(intTempLen - 3, 3)
k = "," & j & k
intTemp = i
Console.WriteLine(k)
intTempLen = intTemp.ToString.Length
Loop
If intTemp.ToString = "" Then
k = k.Substring(1, k.Length)
Else
k = intTemp & k
End If
yourInteger = k

Leave a Reply

Your email address will not be published.