Business class for converting Decimal Number to Words using C# and VB.Net in ASP.Net

Last Reply 6 days ago By dharmendr

Posted 6 days ago

Create Class for Number to Words (Including Paisa) and used it in different page for label.

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

 I Think here is a conflict, please correct the code and make it into class and describe how to use class for number to words

My result is in shown in the label now i want to convert this decimal value into words

ex. - 123.35 --> One Hundred and Twenty three Rupees and Thirty Five Paisa.

Your above code is not able to understand

please correct

You are viewing reply posted by: dharmendr 6 days ago.
Posted 6 days ago

Hi AnkitPal,

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

Class

C#

public static class NumberToWord
{
    public static 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)(Math.Round((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 static 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 static 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 Class NumberToWord
    Public Shared Function NumberToWords(ByVal doubleNumber As Double) As String
        Dim beforeFloatingPoint As Integer = CInt(Math.Floor(doubleNumber))
        Dim beforeFloatingPointWord As String = String.Format("{0} Rupees", NumberToWords(beforeFloatingPoint))
        Dim afterFloatingPointWord As String = String.Format("{0} Paisa Only.",
                                            SmallNumberToWord(CInt((Math.Round((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 Shared 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 Shared 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
End Class

HTML

<asp:TextBox ID="txtNumber" runat="server" />
<asp:Button Text="Convert" runat="server" OnClick="OnConvert" /><br />
<asp:Label ID="lblWord" runat="server" />

Code

C#

protected void OnConvert(object sender, EventArgs e)
{
    lblWord.Text = "";
    if (!string.IsNullOrEmpty(txtNumber.Text.Trim()))
    {
        lblWord.Text = NumberToWord.NumberToWords(Convert.ToDouble(txtNumber.Text.Trim()));
    }
}

VB.Net

Protected Sub OnConvert(ByVal sender As Object, ByVal e As EventArgs)
    lblWord.Text = ""
    If Not String.IsNullOrEmpty(txtNumber.Text.Trim()) Then
        lblWord.Text = NumberToWord.NumberToWords(Convert.ToDouble(txtNumber.Text.Trim()))
    End If
End Sub

Screenshot