 |
|
 |
But i had implemented it in this way
public static string Number2Word(long lNumber)
{
string[] ones = {"One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine ","Ten ",
"Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Ninteen "
};
string[] tens = { "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninty " };
if (lNumber == 0)
return ("");
if (lNumber < 0)
{
lNumber *= -1;
}
if (lNumber < 20)
{
return ones[lNumber - 1];
}
if (lNumber <= 99)
{
return tens[(lNumber / 10) - 2] + Number2Word(lNumber % 10);
}
if (lNumber < 1000)
{
return Number2Word(lNumber / 100) + "Hundred " + Number2Word(lNumber % 100);
}
if (lNumber < 100000)
{
return Number2Word(lNumber / 1000) + "Thousand " + Number2Word(lNumber % 1000);
}
if (lNumber < 10000000)
{
return Number2Word(lNumber / 100000) + "Lakh " + Number2Word(lNumber % 100000);
}
if (lNumber < 1000000000)
{
return Number2Word(lNumber / 10000000) + "Crore " + Number2Word(lNumber % 10000000);
}
return "";
}
|
|
|
|
 |
|
 |
Fails on 1020
result is " Thousand and Hundred Twenty" instead of " Thousand and Twenty" ..
pls send me rightable code ..
senthil@webrks.com
|
|
|
|
 |
|
 |
Can you please format your code with proper syntax?
Use <pre lang="C#">...</pre> tag and use proper space.
This way your code will be more visible & readable.
Don't forget to Click on [Vote] and [Good Answer] on the posts that helped you.
Regards - Kunal Chowdhury | Software Developer | Chennai | India | My Blog | My Tweets | Silverlight Tutorial
|
|
|
|
 |
|
 |
Was gonna write my own but boom it's already here. Thanks for sharing.
|
|
|
|
 |
|
 |
Fails on 10020
result is "Ten Thousand and Hundred Twenty" instead of "Ten Thousand and Twenty"
xxxx xx xxxx xx xx
|
|
|
|
 |
|
 |
The accounting people here complained about the cents so I modified the code to put "thirty" and whatever cents at the end of the texts
I'm a new guy to C# and stuff but here it is
namespace custom.util
{
public class NumberToEnglish
{
public String changeNumericToWords(double numb)
{
String num = numb.ToString();
return changeToWords(num, false);
}
public String changeCurrencyToWords(String numb)
{
return changeToWords(numb, true);
}
public String changeNumerictoWords(String numb)
{
return changeToWords(numb, false);
}
public String changeCurrencyToWords(double numb)
{
return changeToWords(numb.ToString(), true);
}
private String changeToWords(String numb, bool isCurrency)
{
String val = "", wholeNo = numb, points = "", andStr = "", pointStr = "";
String endStr = (isCurrency) ? ("Only") : ("");
try
{
int decimalPlace = numb.IndexOf(".");
if (decimalPlace > 0)
{
wholeNo = numb.Substring(0, decimalPlace);
points = numb.Substring(decimalPlace + 1);
if (Convert.ToInt32(points) > 0)
{
andStr = (isCurrency) ? (" dollars and ") : ("point");//just to separate whole numbers from points/cents
endStr = (isCurrency) ? (" Cents " + endStr) : ("");
pointStr = translateCents(points);
}
}
val = String.Format("{0} {1}{2}{3}", translateWholeNumber(wholeNo).Trim(), andStr, pointStr, endStr);
}
catch
{
;
}
return val;
}
private String translateWholeNumber(String number)
{
string word = "";
try
{
bool beginsZero = false;//tests for 0xx
bool isDone = false;//test if already translated
double dblAmt = (Convert.ToDouble(number));
//if ((dblAmt > 0) && number.StartsWith("0"))
if (dblAmt > 0)
{
beginsZero = number.StartsWith("0");
int numDigits = number.Length;
int pos = 0; //store digit grouping
String place = "";//digit grouping name:hundreds,thousand, etc...
switch (numDigits)
{
case 1://ones' range
word = ones(number);
isDone = true;
break;
case 2://tens' range
word = tens(number);
isDone = true;
break;
case 3://hundreds' range
pos = (numDigits % 3) + 1;
place = " hundred ";
break;
case 4://thousands' range
case 5:
case 6:
pos = (numDigits % 4) + 1;
place = " thousand ";
break;
case 7://millions' range
case 8:
case 9:
pos = (numDigits % 7) + 1;
place = " Million ";
break;
case 10://Billions' range
pos = (numDigits % 10) + 1;
break;
default:
isDone = true;
break;
}
if (!isDone)
{
//if translation is not done, continue...(recursion comes in now!!)
word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos));
// check for trailing zeros
if (beginsZero) word = " and " + word.Trim();
}
}
}
catch
{
;
}
return word.Trim();
}
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;
}
return name;
}
private String translateCents(String cents)
{
String cts = "", digit = "", engOne = "";
/*for (int i = 0; i < cents.Length; i++)
{
digit = cents[i].ToString();
if (digit == "0")
{
engOne = "Zero";
}
else
{
engOne = ones(digit);
//engOne = tens(digit);
}
cts = cts + " " + engOne;
}/**/
//MessageBox.Show(cents[0].ToString() + " " + cents[1].ToString());
if (cents[0].ToString().Equals("0"))
{
cts = "Zero and " + ones(cents[1].ToString());
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents[0].ToString().Equals("2"))
{
cts = "Twenty" + ones(cents[1].ToString());
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents[0].ToString().Equals("3"))
{
cts = "Thirty" + ones(cents[1].ToString());
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents[0].ToString().Equals("4"))
{
cts = "Forty" + ones(cents[1].ToString());
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents[0].ToString().Equals("5"))
{
cts = "Fifty" + ones(cents[1].ToString());
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents[0].ToString().Equals("6"))
{
cts = "Sixty" + ones(cents[1].ToString());
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents[0].ToString().Equals("7"))
{
cts = "Seventy" + ones(cents[1].ToString());
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents[0].ToString().Equals("8"))
{
cts = "Eighty" + ones(cents[1].ToString());
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents[0].ToString().Equals("9"))
{
cts = "Ninety" + ones(cents[1].ToString());
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents.Equals("11"))
{
cts = "Eleven";
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents.Equals("12"))
{
cts = "Twelve";
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents.Equals("13"))
{
cts = "Thirteen";
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents == "14")
{
cts = "Fourteen";
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents == "15")
{
cts = "Fifteen";
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents == "16")
{
cts = "Sixteen";
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents == "17")
{
cts = "Seventeen";
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents == "18")
{
cts = "Eighteen";
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
if (cents == "19")
{
cts = "Nineteen";
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
}
//MessageBox.Show(" cents " + cts + " " + cents[0].ToString() + cents[1].ToString());
return cts;
}
}
}
|
|
|
|
 |
|
 |
There seems to be a problem when there are zero hundreds or zero thousands as the word hundred or thousand is still printed. I did not fix it as I adapeted the solution referred to in the previous post.
|
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Just hacked it to support negative numbers. In the changeToWords function, replace the top 2 lines with:
String val = "", wholeNo = numb, points = "", andStr = "", pointStr="", prefix="";
String endStr = (isCurrency) ? ("Only") : ("");
double dblAmt = Convert.ToDouble(numb);
if(dblAmt < 0)
{
wholeNo = (dblAmt * -1).ToString();
prefix = "Minus ";
}
Then replace the 3rd last line that starts with 'val =' with:
val = String.Format("{0}{1} {2}{3} {4}", prefix, translateWholeNumber(wholeNo).Trim(), andStr, pointStr, endStr);
Hope this helps someone.
|
|
|
|
 |
|
 |
I have been looking for this! I am busy modifying changeCurrencyToWords to read cents as whole numbers like 12 to twelve not "one two".
I really appreciate your assistance
I remain joe! - South Africa
"Only the best"
|
|
|
|
 |
|
 |
your article helps me much.
teligaurav@gmail.com
teligaurav@gmail.com
|
|
|
|
 |
|
 |
Hey fbyk,
You should check out the 'real world' sample code at the Data and Object Factory site. You might be able to refactor your solution into more resuable components.
-Bill
|
|
|
|
 |
|
 |
Thanks Bill!
I'll explore that option as well.
|
|
|
|
 |