Click here to Skip to main content
15,028,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i ve some numbers in int a, and i hv one text box on loading the nuber in the integer should display like characters.
EG:
int a=89;
while loading in text box it should come like eighty nine;




pls help in solving this and thanks in advance
Posted

First of all I took two text boxes named txtNum and txtword and then a button.
On click Of button the number which you entered in txtNum will be converted into words.

Below is the aspx page:

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NumPage.aspx.cs" Inherits="Number_Word.NumPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Enter the number :
        <asp:TextBox ID="txtNum" runat="server" Width="199px"></asp:TextBox>
        <br />
        <br />
        Words:&nbsp;&nbsp;
        <asp:TextBox ID="txtWord" runat="server" TextMode="MultiLine" Width="250px"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="btnWords" runat="server" onclick="btnWords_Click" Text="Click to get in words" />
    </div>
    </form>
</body>
</html>


.cs file

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Number_Word
{
    public partial class NumPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnWords_Click(object sender, EventArgs e)
        {
            //int iNum;
            //retWord(Int32.Parse(txtNum.Text));
            txtWord.Text = retWord(Int32.Parse(txtNum.Text));
        }
        public string retWord(int number)
        {

            if (number == 0) return "Zero";

            if (number == -2147483648) return "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight";

            int[] num = new int[4];

            int first = 0;

            int u, h, t;

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            if (number < 0)
            {

                sb.Append("Minus");

                number = -number;

            }

            string[] words0 = { "", "One", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine " };

            string[] words = { "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen " };

            string[] words2 = { "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety " };

            string[] words3 = { "Thousand ", "Lakh ", "Crore " };

            num[0] = number % 1000; // units

            num[1] = number / 1000;

            num[2] = number / 100000;

            num[1] = num[1] - 100 * num[2]; // thousands

            num[3] = number / 10000000; // crores

            num[2] = num[2] - 100 * num[3]; // lakhs



            for (int i = 3; i > 0; i--)
            {

                if (num[i] != 0)
                {

                    first = i;

                    break;

                }

            }

            for (int i = first; i >= 0; i--)
            {

                if (num[i] == 0) continue;

                u = num[i] % 10; // ones

                t = num[i] / 10;

                h = num[i] / 100; // hundreds

                t = t - 10 * h; // tens

                if (h > 0) sb.Append(words0[h] + "Hundred ");

                if (u > 0 || t > 0)
                {

                    if (h > 0 || i == 0) sb.Append("and ");

                    if (t == 0)

                        sb.Append(words0[u]);

                    else if (t == 1)

                        sb.Append(words[u]);

                    else

                        sb.Append(words2[t - 2] + words0[u]);

                }

                if (i != 0) sb.Append(words3[i - 1]);

            }

            return sb.ToString().TrimEnd();

        }
    }
}






Hope this will help you.

Thanks
Praveen N
   
v4
Comments
CodeHawkz 11-May-11 6:10am
   
Nice code. But would have been so much better if you posted this an article and shared the link here :) I bet there are a lot of newbies (like me :D) looking for a simple code like this.
[no name] 11-May-11 11:15am
   
well i thought of the same. next time will surly do it. :)
[no name] 11-May-11 11:17am
   
even i thought of posting the screen shot of output. But i was not able to paste the screen shot here.
   
Comments
   
I guess you're right, my 5.
I probably did not understand this answer.
--SA
Olivier Levrey 11-May-11 4:22am
   
Google rules. My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900