locked
convert currency number in to words RRS feed

  • Question

  • User-807418713 posted

    Hello

    How To Convert Label value currency in too Word

    For example My Label1 has like this 130,560 INR

    I want to show in Label2 One Lakh Thirty Thousand Five Hundared Sixty Only

    How to do so 

    Thanking You

    Wednesday, September 4, 2019 6:59 PM

Answers

  • User288213138 posted

    Hi Gopi.MCA,

    You can try below code.  

    Calculate the value in the label and return different results according to different situations.

    The code:

    <asp:Label ID="Label1" runat="server" Text="130,560"></asp:Label><br />
                <asp:Label ID="Label2" runat="server"></asp:Label><br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    protected void Button1_Click(object sender, EventArgs e)
            {
               
                Label2.Text = NumberToWords(Convert.ToInt32(Label1.Text.Replace(",","")))+" only";
    
            }
            public static string NumberToWords(int number)
            {
                if (number == 0)
                    return "zero";
    
                if (number < 0)
                    return "minus " + NumberToWords(Math.Abs(number));
    
                string words = "";
    
                if ((number / 1000000) > 0)
                {
                    words += NumberToWords(number / 1000000) + " million ";
                    number %= 1000000;
                }
                if ((number / 100000) > 0)
                {
                    words += NumberToWords(number / 100000) + " Lakh  ";
                    number %= 100000;
                }
    
                if ((number / 1000) > 0)
                {
                    words += NumberToWords(number / 1000) + " thousand ";
                    number %= 1000;
                }
    
                if ((number / 100) > 0)
                {
                    words += NumberToWords(number / 100) + " hundred ";
                    number %= 100;
                }
    
                if (number > 0)
                {           
                    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;
            }
    

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 5, 2019 3:27 AM

All replies

  • User409696431 posted

    You would have to write code to parse it and generate the words.  There is no automatic conversion.

    Wednesday, September 4, 2019 8:11 PM
  • User288213138 posted

    Hi Gopi.MCA,

    You can try below code.  

    Calculate the value in the label and return different results according to different situations.

    The code:

    <asp:Label ID="Label1" runat="server" Text="130,560"></asp:Label><br />
                <asp:Label ID="Label2" runat="server"></asp:Label><br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    protected void Button1_Click(object sender, EventArgs e)
            {
               
                Label2.Text = NumberToWords(Convert.ToInt32(Label1.Text.Replace(",","")))+" only";
    
            }
            public static string NumberToWords(int number)
            {
                if (number == 0)
                    return "zero";
    
                if (number < 0)
                    return "minus " + NumberToWords(Math.Abs(number));
    
                string words = "";
    
                if ((number / 1000000) > 0)
                {
                    words += NumberToWords(number / 1000000) + " million ";
                    number %= 1000000;
                }
                if ((number / 100000) > 0)
                {
                    words += NumberToWords(number / 100000) + " Lakh  ";
                    number %= 100000;
                }
    
                if ((number / 1000) > 0)
                {
                    words += NumberToWords(number / 1000) + " thousand ";
                    number %= 1000;
                }
    
                if ((number / 100) > 0)
                {
                    words += NumberToWords(number / 100) + " hundred ";
                    number %= 100;
                }
    
                if (number > 0)
                {           
                    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;
            }
    

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 5, 2019 3:27 AM
  • User-807418713 posted

    hello

    in old asp.net version i got thos error

    Line 76:             if (number > 0)
    Line 77:             {           
    Line 78:                 string unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
    Line 79:                 string tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
    Line 80: 
    Sunday, September 8, 2019 1:08 PM
  • User288213138 posted

    Hi Gopi.MCA,

    i got thos error

    Please post your detailed error message.

    Best regards.

    Monday, September 9, 2019 1:50 AM
  • User-807418713 posted

    Hello

    This is the error

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS1031: Type expected

    Source Error:

    Line 267:            if (number > 0)
    Line 268:            {           
    Line 269:                var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
    Line 270:                var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

    Monday, September 9, 2019 7:30 AM
  • User288213138 posted

    Hi Gopi.MCA, 

    I guess the problem caused by the C# version

    Because beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit "type" var.

    Please try below code:

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

    please let me know if you solve the problem.

    Best regards,

    Sam

    Monday, September 9, 2019 7:53 AM