Important alert: (current site time 7/15/2013 8:06:05 AM EDT)
 

VB icon

convert numbers to words for a check

Email
Submitted on: 1/18/2011 8:20:22 PM
By: Brian Davis 
Level: Intermediate
User Rating: By 1 Users
Compatibility: C#, ASP.NET
Views: 2794
(About the author)
 
     When I was working with one company, I needed to make a windows app that printed a check...I made this dll to convert the numeric amount to a worded amount on the check.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: convert numbers to words for a check
// Description:When I was working with one company, I needed to make a windows app that printed a check...I made this dll to convert the numeric amount to a worded amount on the check.
// By: Brian Davis
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=8170&lngWId=10//for details.//**************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConvertNumberToWords
{
public class ConvertThis
{
public String changeCurrencyToWords(String numb)
{
return changeToWords(numb);
}
private String changeToWords(String numb)
{
int decimalPlace = 0;
String result = "";
int c = numb.Length;
if (numb.Contains(".") == true)
{
decimalPlace = numb.IndexOf(".");
}
else
{
numb = numb + ".00";
decimalPlace = numb.IndexOf(".");
}
if (decimalPlace == c)
{
numb = numb + "00";
}
String wn = numb.Substring(0, decimalPlace);
int wn_1 = wn.Length;
String cents = numb.Substring(decimalPlace + 1);
switch (wn_1)
{
//ones
case 1:
result = ones(numb.Substring(0, wn_1)) + " dollars " + cents + "/100";
break;
//tens
case 2:
result = tens(numb.Substring(0, wn_1)) + " dollars " + cents + "/100";
break;
//hundreds
case 3:
result = ones(numb.Substring(0, 1)) + " hundred " + tens(numb.Substring(1, 2)) + " dollars " + cents + "/100";
break;
//thousands
case 4:
result = ones(numb.Substring(0, 1)) + " thousand " + ones(numb.Substring(1, 1)) + " hundred " + tens(numb.Substring(2, 2)) + " dollars " + cents + "/100";
break;
//ten thousands
case 5:
result = tens(numb.Substring(0, 2)) + " thousand " + ones(numb.Substring(2, 1)) + " hundred " + tens(numb.Substring(3, 2)) + " dollars " + cents + "/100";
break;
//hundred thousands
case 6:
result = ones(numb.Substring(0, 1)) + " hundred " + tens(numb.Substring(1, 2)) + " thousand " + ones(numb.Substring(3, 1)) + " hundred " + tens(numb.Substring(4, 2)) + " dollars " + cents + "/100";
break;
// million
case 7:
result = ones(numb.Substring(0, 1)) + " million " + ones(numb.Substring(1, 1)) + " hundred " + tens(numb.Substring(2, 2)) + " thousand " + ones(numb.Substring(4, 1)) + " hundred " + tens(numb.Substring(5, 2)) + " dollars " + cents + "/100";
break;
// ten millions
case 8:
result = tens(numb.Substring(0,2)) + " million " + ones(numb.Substring(2,1)) + " hundred " + tens(numb.Substring(3,2)) + " thousand " + ones(numb.Substring(5,1)) + " hundred " + tens(numb.Substring(6,2)) + " dollars " + cents + "/100";
break;
case 9:
result = ones(numb.Substring(0,1)) + " hundred " + tens(numb.Substring(1,2)) + " million " + ones(numb.Substring(3,1)) + " hundred " + tens(numb.Substring(4,2)) + " thousand " + ones(numb.Substring(6,1)) + " hundred " + tens(numb.Substring(7,2)) + " dollars " + cents + "/100";
break;
case 10:
result = " this is a lot of money contact IT for a fix ";
break;
case 11:
result = " this is a lot of money contact IT for a fix ";
break;
case 12:
result = " this is a lot of money contact IT for a fix ";
break;
}
return result;
}
private String tens(String digit)
{
int digt = Convert.ToInt32(digit);
String name = null;
switch (digt)
{
case 10:
name = "Ten";
break;
case 11:
name = "Eleven";
break;
case 12:
name = "Twelve";
break;
case 13:
name = "Thirteen";
break;
case 14:
name = "Fourteen";
break;
case 15:
name = "Fifteen";
break;
case 16:
name = "Sixteen";
break;
case 17:
name = "Seventeen";
break;
case 18:
name = "Eighteen";
break;
case 19:
name = "Nineteen";
break;
case 20:
name = "Twenty";
break;
case 30:
name = "Thirty";
break;
case 40:
name = "Forty";
break;
case 50:
name = "Fifty";
break;
case 60:
name = "Sixty";
break;
case 70:
name = "Seventy";
break;
case 80:
name = "Eighty";
break;
case 90:
name = "Ninety";
break;
default:
if (digt > 0)
{
name = tens(digit.Substring(0, 1) + "0") + ones(digit.Substring(1));
}
break;
}
return name;
}
private String ones(String digit)
{
int digt = Convert.ToInt32(digit);
String name = "";
switch (digt)
{
case 1:
name = "One";
break;
case 2:
name = "Two";
break;
case 3:
name = "Three";
break;
case 4:
name = "Four";
break;
case 5:
name = "Five";
break;
case 6:
name = "Six";
break;
case 7:
name = "Seven";
break;
case 8:
name = "Eight";
break;
case 9:
name = "Nine";
break;
case 0:
name = "Zero";
break;
}
return name;
}
}
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.