Converting Numbers To String Using ASP.NET

Background


Usually when you are working on financial applications, particularly on a Financial Transaction Report Module then their is a need to convert numbers into strings. Suppose their is a bill print module in which you must convert an Amount entered into a textbox to a string then instead of manually entering the Amount, in a word you can directly convert the amount entered to the textbox to a string.
So considering the above requirement  have written this article to demonstrate how to convert any number to a string, so let us start it with the Basics.
Introduction

This article is very useful to all particularly who are working on Financial projects but by writing this article I have also focused on Beginners so let us start it with the basics.
What is a String?

A String is a sequence of charcters enclosed within double quatation marks.
Example

"vithal wadje" ,   "C#" , "Mumbai"  "Five hundred" etc.
What is a Number?

Any digits between 0-9 including floats, decimals, numerals which are countable is called a Number.
Example

523656, 452.01 165.00452.01 165.00  etc.
Now I hope you understand the concept of String and Number. Now we are developing the application to convert these numbers into strings with one financial screen. So let us start it step-by-step.
Create a website as:
  1. Start - All Programs - Microsoft Visual Studio 2010 
  2. File - New Website-C# - Empty website (to avoid adding a master page)
  3. Give the web site a name such as ConvertingNumbersToWord and specify the location
  4. Then right-click on Solution Explorer - Add New Item-Default.aspx page
  5. Drag a drop one Textbox for entering the value, one button to perform the action and one label to show the results in a <form> tag region
After the <form> tag region code will look as:

<form id="form1" runat="server">
    <table>
   <tr>
   <td>Enter Amount</td>
    </tr
    <tr>
   < td >
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
         </ td >
         </tr
           <tr>
         < td >
             <asp:Label ID="Label1" runat="server" Text="Label" BackColor="Gray" 
                 Font-Bold="True" Font-Italic="False" ForeColor="#CC3300"></asp:Label>
       
         </ td >
         </tr
          <tr>
         < td  align="center" >
         
          <asp:Button ID="Button1" runat="server" Text="Convert" onclick="Button1_Click" /> 
         </ td >
         
          </tr
    </table>
    </div>
    </form>

Then switch to view code and create a  method Named  Initialize() to hold the strings, as:

  private void Initialize() 
    {
        SNu[0] = ""; 
        SNu[1] = "One";
        SNu[2] = "Two";        //to hold string related to numbers which is called on page load
        SNu[3] = "Three"
        SNu[4] = "Four";
        SNu[5] = "Five"
        SNu[6] = "Six"
        SNu[7] = "Seven"
        SNu[8] = "Eight"
        SNu[9] = "Nine"
        SNu[10] = "Ten"
        SNu[11] = "Eleven";
        SNu[12] = "Twelve";
        SNu[13] = "Thirteen";
        SNu[14] = "Fourteen";
        SNu[15] = "Fifteen";
        SNu[16] = "Sixteen";
        SNu[17] = "Seventeen"
        SNu[18] = "Eighteen"
        SNu[19] = "Nineteen";
        SNt[2] = "Twenty";
        SNt[3] = "Thirty"
        SNt[4] = "Forty";
        SNt[5] = "Fifty"
        SNt[6] = "Sixty"
        SNt[7] = "Seventy";
        SNt[8] = "Eighty"
        SNt[9] = "Ninety";
        US[1] = "";
        US[2] = "Thousand";
        US[3] = "Million"
        US[4] = "Billion";
        US[5] = "Trillion";
        US[6] = "Quadrillion";
        US[7] = "Quintillion";
        US[8] = "Sextillion";
        US[9] = "Septillion";
        US[10] = "Octillion"
    }

and call this method on page load as:
 protected void Page_Load(object sender, EventArgs e)
    {
      
        Initialize();   //calling string method
        
    }

Then double-click on the button and use this code for the button click event:
 protected void Button1_Click(object sender, EventArgs e)
{
 if (TextBox1.Text == "")
        {
            Label1.Text = "Enter Amount";
        }
        else
        {
            string currency = "Rupees ";               //  for adding a prefix before converted string          
            if (Convert.ToDouble(TextBox1.Text) == 0)
            {
       
                Label1.Text = "Null Value";             //if user enters null value
                return;
            }
            if (Convert.ToDouble(TextBox1.Text) < 0)
            {
                Label1.Text = "Invalid Value";
             
                return;
            }
 string[] no = TextBox1.Text.Split('.');
            if ((no[0] != null) && (no[1] != "00"))      //spliting the texbox value to check if any decimal point is added after number  
            {
                Number = no[0]; deciml = no[1 ];
                _number = (NameOfNumber(Number));
                _deciml = (NameOfNumber(deciml ));
              
                Label1.Text = currency + _number.Trim() + " and " + _deciml.Trim() + currency;
            }
            if ((no[0] != null) && (no[1] == "00"))      // check if user does not input decimal, value
            {
                Number = no[0];
                _number = (NameOfNumber(Number));
             
                Label1.Text = currency + _number + "Only"; // showing whole  result with suffix
            }
            if ((Convert.ToDouble(no[0]) == 0) && (no[1] != null))
            {
                deciml = no[1];
                _deciml = (NameOfNumber(deciml));
                Label1.Text = _deciml + currency;
               
            }
For more code please download the sample application zip file, then run the application and input a value into the textbox which looks as in the following image:
outputscreen.png
So the output of the application is as shown in the above image after running the application.
Note
When you enter the value in the textbox enter with decimal points.
Example

5823.00 , 45215.54 etc.
Summary

The above article is very useful in any project depending on your requirements, for both beginner as well as intermediate readers to understand. I have provided more examples with explanations and comments..I hope this article helps all readers. If you have any suggestion then please contact me.


Similar Articles