SCProject.Biz CODER logoLarge Numbers to Words




This allows conversions from up to 66 digit numbers to Short Scale numbers (i.e Millions, Billions, etc).

The numbers are entered as Strings via a TextBox. As the largest standard .Net Integer number is ULong with a MaxValue of 18,446,744,073,709,551,615 and there is no need for using a true numeric Type, this app. uses Strings to store the (max) 3 digit parts of the number.

After checking the user input contains only numeric digits, the number is passed to a new instance of the xl_Integer Class.




OOP - Short Scale numbers



Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If New Regex("^\d+$").IsMatch(TextBox1.Text) Then
            Dim xlInt As New xl_Integer(TextBox1.Text)
            TextBox2.Text = xlInt.toFormattedString
            TextBox3.Text = xlInt.toWords
        Else
            TextBox2.Text = ""
            TextBox3.Text = ""
            MsgBox("Invalid Input")
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
    End Sub

End Class



This is the xl_Integer Class, which is an Information Specialist utility Class. Each new instance of this Class is passed a numeric String. That String is then split into (max) 3 digit parts.

Invoking the toFormattedString() Function groups the (max) 3 digit parts into a numeric formatted String. Invoking the toWords() Function converts the numeric parts into a String using Short Scale number names.



Public Class xl_Integer

    Dim parts As New List(Of String)

    Public Sub New(ByVal largeNumber As String)
        For x As Integer = largeNumber.Length To 0 Step -3
            parts.Add(largeNumber.Substring(Math.Max(x - 3, 0), Math.Min(3, x)))
        Next
    End Sub

    Public Function toFormattedString() As String
        Dim reversedParts(parts.Count - 1) As String
        parts.CopyTo(reversedParts)
        reversedParts = reversedParts.Reverse().ToArray

        Return String.Join(",", reversedParts).TrimStart(","c, " "c)

    End Function

    Public Function toWords() As String
        Dim strSuffix() As String = {"", " Thousand", " Million", " Billion", _
                                     " Trillion", " Quadrillion", " Quintillion", " Sextillion", _
                                     " Septillion", " Octillion", " Nonillion", " Decillion", _
                                     " Undecillion", " Duodecillion", " Tredecillion", " Quattuordecillion", _
                                     " Quindecillion", " Sexdecillion", " Septendecillion", " Octodecillion", _
                                     " Novemdecillion", " Vigintillion"}

        Dim output As String = ""

        For x As Integer = 0 To parts.Count - 1
            output = If(Val(parts(x)) > 0, CStr(Val(parts(x))) & strSuffix(x) & ", " & output, output)
        Next

        Return output.TrimEnd(","c, " "c)

    End Function

End Class



The example project is available for download here...