Dim TestNumbers() As Int = Array As Int(4, 14, 145, 1459, 14592, 1415926, 14159265)
For Each number As Int In TestNumbers
Log(number & TAB & FormatNumberGrouping(number, 4, 3, ".") & TAB & FormatNumberGrouping(number, 9, 3, ",") & TAB & FormatNumberGrouping(number, 12, 4, " "))
Next
Sub FormatNumberGrouping(number As Long, mindigits As Int, GroupingDigits As Int, GroupingCharacter As String) As String
Dim sNumber As String = number, i0s As Int = mindigits - sNumber.Length
If i0s > 0 Then sNumber = "0000000000000000".SubString(16 - i0s) & sNumber
Dim iLen As Int = sNumber.Length
If GroupingDigits > 0 And GroupingDigits < iLen And GroupingCharacter <> "" Then
Dim iCount As Int, iRest As Int = iLen Mod GroupingDigits
If iRest > 0 Then iCount = GroupingDigits - iRest 'e.g. GroupingDigits = 3, Length = 4 -> Rest = 1, iCount = 2 -> first GroupingCharacter after 1 digit
Dim sb As StringBuilder : sb.Initialize
For i = 0 To iLen - 1
If iCount > 0 And iCount Mod GroupingDigits = 0 Then sb.Append(GroupingCharacter)
sb.Append(sNumber.CharAt(i))
iCount = iCount + 1
Next
sNumber = sb.ToString
End If
Return sNumber
End Sub