Convert Decimal Number to Words using C# and VB.Net

AnkitPal
 
on Apr 03, 2021 02:08 AM
8246 Views

Convert Number to Words (Including Paisa) and used it in another page for label

Ex. - 123.50 - One Hundred Twenty Three Rupees and Fifty Paisa Only.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Apr 03, 2021 07:08 AM

Hi AnkitPal,

Check this example. Now please take its reference and correct your code.

C#

public string NumberToWords(double doubleNumber)
{
    int beforeFloatingPoint = (int)Math.Floor(doubleNumber);
    string beforeFloatingPointWord = string.Format("{0} rupees", NumberToWords(beforeFloatingPoint));
    string afterFloatingPointWord = string.Format("{0} paisa only.", SmallNumberToWord((int)((doubleNumber - beforeFloatingPoint) * 100), ""));
    if ((int)((doubleNumber - beforeFloatingPoint) * 100) > 0)
    {
        return string.Format("{0} and {1}", beforeFloatingPointWord, afterFloatingPointWord);
    }
    else
    {
        return string.Format("{0} only", beforeFloatingPointWord);
    }
}

private string NumberToWords(int number)
{
    if (number == 0)
        return "zero";

    if (number < 0)
        return "minus " + NumberToWords(Math.Abs(number));

    var words = "";

    if (number / 1000000000 > 0)
    {
        words += NumberToWords(number / 1000000000) + " billion ";
        number %= 1000000000;
    }

    if (number / 1000000 > 0)
    {
        words += NumberToWords(number / 1000000) + " million ";
        number %= 1000000;
    }

    if (number / 1000 > 0)
    {
        words += NumberToWords(number / 1000) + " thousand ";
        number %= 1000;
    }

    if (number / 100 > 0)
    {
        words += NumberToWords(number / 100) + " hundred ";
        number %= 100;
    }

    words = SmallNumberToWord(number, words);

    return words;
}

private string SmallNumberToWord(int number, string words)
{
    if (number <= 0) return words;
    if (words != "")
        words += " ";

    var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
    var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

    if (number < 20)
        words += unitsMap[number];
    else
    {
        words += tensMap[number / 10];
        if ((number % 10) > 0)
            words += " " + unitsMap[number % 10];
    }
    return words;
}

VB.Net

Public Function NumberToWords(ByVal doubleNumber As Double) As String
    Dim beforeFloatingPoint = CInt(Math.Floor(doubleNumber))
    Dim beforeFloatingPointWord = String.Format("{0} rupees", NumberToWords(beforeFloatingPoint))
    Dim afterFloatingPointWord = String.Format("{0} paisa only.", SmallNumberToWord(CInt(((doubleNumber - beforeFloatingPoint) * 100)), ""))

    If CInt(((doubleNumber - beforeFloatingPoint) * 100)) > 0 Then
        Return String.Format("{0} and {1}", beforeFloatingPointWord, afterFloatingPointWord)
    Else
        Return String.Format("{0} only", beforeFloatingPointWord)
    End If
End Function

Private Function NumberToWords(ByVal number As Integer) As String
    If number = 0 Then Return "zero"
    If number< 0 Then Return "minus " & NumberToWords(Math.Abs(number))
    Dim words = ""

    If number / 1000000000 > 0 Then
        words += NumberToWords(number / 1000000000) & " billion "
        number = number Mod 1000000000
    End If

    If number / 1000000 > 0 Then
        words += NumberToWords(number / 1000000) & " million "
        number = number Mod 1000000
    End If

    If number / 1000 > 0 Then
        words += NumberToWords(number / 1000) & " thousand "
        number = number Mod 1000
    End If

    If number / 100 > 0 Then
        words += NumberToWords(number / 100) & " hundred "
        number = number Mod 100
    End If

    words = SmallNumberToWord(number, words)
    Return words
End Function

Private Function SmallNumberToWord(ByVal number As Integer, ByVal words As String) As String
    If number <= 0 Then Return words
    If words<> "" Then words += " "
    Dim unitsMap = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }
    Dim tensMap = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }

    If number< 20 Then
        words += unitsMap(number)
    Else
        words += tensMap(number / 10)
        If(number Mod 10) > 0 Then words += " " & unitsMap(number Mod 10)
    End If

    Return words
End Function