How to convert numbers from 0 to 999,999,999,999,999 (up to 999 trillion) into English words in VB.NET

1 Answer

0 votes
Imports System

Module NumberToWords

    '
    ' Convert numbers to English words in VB.NET
    ' ------------------------------------------
    ' Supports numbers from 0 to 999,999,999,999,999.
    '
    ' Strategy:
    ' - Break the number into named groups: trillions, billions, millions,
    '   thousands, hundreds.
    ' - Convert each 0–999 group using a helper method.
    ' - Append group names ("trillion", "billion", etc.) when needed.
    ' - Build the final string with proper spacing.
    '

    Private ReadOnly Ones() As String = {
        "zero", "one", "two", "three", "four",
        "five", "six", "seven", "eight", "nine",
        "ten", "eleven", "twelve", "thirteen", "fourteen",
        "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
    }

    Private ReadOnly TensWords() As String = {
        "", "", "twenty", "thirty", "forty",
        "fifty", "sixty", "seventy", "eighty", "ninety"
    }

    '
    ' appendWord:
    ' Adds a word to the output with proper spacing.
    '
    Private Sub AppendWord(ByRef outStr As String, word As String)
        If word <> "" Then
            If outStr <> "" Then outStr &= " "
            outStr &= word
        End If
    End Sub

    '
    ' convertHundreds:
    ' Converts numbers from 0 to 999 into words.
    '
    Private Sub ConvertHundreds(n As Long, ByRef outStr As String)
        If n = 0 Then Exit Sub

        If n >= 100 Then
            AppendWord(outStr, Ones(n \ 100))
            AppendWord(outStr, "hundred")
            n = n Mod 100
            If n > 0 Then AppendWord(outStr, "and")
        End If

        If n >= 20 Then
            AppendWord(outStr, TensWords(n \ 10))
            If (n Mod 10) > 0 Then AppendWord(outStr, Ones(n Mod 10))
        ElseIf n > 0 Then
            AppendWord(outStr, Ones(n))
        End If
    End Sub

    '
    ' numberToWords:
    ' Converts any number from 0 to 999,999,999,999,999 into English words.
    '
    Public Function NumberToWords(num As Long) As String
        If num = 0 Then Return "zero"

        Dim result As String = ""

        Dim trillions As Long = num \ 1000000000000L
        Dim billions As Long = (num \ 1000000000L) Mod 1000
        Dim millions As Long = (num \ 1000000L) Mod 1000
        Dim thousands As Long = (num \ 1000L) Mod 1000
        Dim hundreds As Long = num Mod 1000

        If trillions > 0 Then
            ConvertHundreds(trillions, result)
            AppendWord(result, "trillion")
        End If

        If billions > 0 Then
            ConvertHundreds(billions, result)
            AppendWord(result, "billion")
        End If

        If millions > 0 Then
            ConvertHundreds(millions, result)
            AppendWord(result, "million")
        End If

        If thousands > 0 Then
            ConvertHundreds(thousands, result)
            AppendWord(result, "thousand")
        End If

        If hundreds > 0 Then
            ConvertHundreds(hundreds, result)
        End If

        Return result
    End Function

    '
    ' Main:
    ' Demonstrates usage with multiple test numbers.
    '
    Sub Main()
        Dim tests() As Long = {
            1234,
            12345,
            123456,
            1234567,
            123456789,
            12345678901L,
            1234567890123L,
            999999999999999L
        }

        For Each n In tests
            Console.WriteLine($"{n} -> {NumberToWords(n)}")
        Next
    End Sub

End Module

					
'
' run:
'
' 1234 -> one thousand two hundred and thirty four
' 12345 -> twelve thousand three hundred and forty five
' 123456 -> one hundred and twenty three thousand four hundred and fifty six
' 1234567 -> one million two hundred and thirty four thousand five hundred and sixty seven
' 123456789 -> one hundred and twenty three million four hundred and fifty six thousand seven hundred and eighty nine
' 12345678901 -> twelve billion three hundred and forty five million six hundred and seventy eight thousand nine hundred and one
' 1234567890123 -> one trillion two hundred and thirty four billion five hundred and sixty seven million eight hundred and ninety thousand one hundred and twenty three
' 999999999999999 -> nine hundred and ninety nine trillion nine hundred and ninety nine billion nine hundred and ninety nine million nine hundred and ninety nine thousand nine hundred and ninety nine
'

 



answered Sep 5 by avibootz
edited Sep 5 by avibootz

Related questions

...