using System; using System.Collections.Generic; using System.Text;
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { while (true) { PayrollAmount pay = new PayrollAmount(); Console.Write("Enter Number: "); decimal num = Convert.ToDecimal(Console.ReadLine()); Console.WriteLine(pay.ToText(num)); } } }
class PayrollAmount { private enum ****s { Ten = 10, //diez Eleven, //once Twelve, //doce Thir****, //trece Four****, //catorce Fif****, //quince Six****, //dieciseis Seven****, //diecisiete Eigh****, //dieciocho Nine**** //diecinueve };
private enum Tens { Twenty = 20, //veinte Thirty = 30, //trenta Forty = 40, //cuarenta Fifty = 50, //cincuenta Sixty = 60, //sesenta Seventy = 70, //setenta Eighty = 80, //ochenta Ninety = 90 //noventa };
private enum Ones { Zero, // cero One, // uno Two, // dos Three, //tres Four, //quatro Five, //cinco Six, // seis Seven, //siete Eight, //ocho Nine //nueve };
internal string ToText(decimal amount) { [edit] moved these down where they belong string hundred = " Hundred"; //ciento string thousand = " Thousand"; //mil string dollars = " Dollars"; //dolares string pennies = " Cents"; //centavos string zero = "Zero"; //cero string number = string.Empty;
if (amount >= 1 && amount < 2) dollars = " Dollar"; //dolar
int tenThous = (int)amount / 10000; amount -= (tenThous * 10000);
int thous = (int)amount / 1000; amount -= (thous * 1000);
int hundreds = (int)amount / 100; amount -= (hundreds * 100);
int tens = (int)amount / 10; amount -= (tens * 10);
int ones = (int)amount; amount -= (ones);
int tenCents = (int)(amount * 100); int cents = tenCents % 10; tenCents /= 10;
if (tenCents == 0 && cents == 1) pennies = " Cent"; //centavo
if (tenThous > 0) { number += BuildTens(tenThous, thous); number += thousand; } else if (thous > 0) { number += BuildSingles(thous, thousand); } if (hundreds > 0) { if (number != string.Empty) number += " ";
number += BuildSingles(hundreds, hundred); number += " "; } else if(number != string.Empty) { number += " "; }
if (tens > 0) { number += BuildTens(tens, ones); } else if (ones > 0) { number += BuildSingles(ones, string.Empty); } if (number == string.Empty) number = zero;
number += dollars + " and "; //" y "
if (tenCents > 0) { number += BuildTens(tenCents, cents); } else if (cents > 0) { number += BuildSingles(cents, string.Empty); }
if (tenCents == 0 && cents == 0) { number += zero + pennies; } else { number += pennies; } return number; }
private string BuildTens(int tens, int singles) { string result = string.Empty;
if (tens > 1) //between 20 and 90 { switch (tens) { case (int)Ones.Two: result += Tens.Twenty; break;
case (int)Ones.Three: result += Tens.Thirty; break;
case (int)Ones.Four: result += Tens.Forty; break;
case (int)Ones.Five: result += Tens.Fifty; break;
case (int)Ones.Six: result += Tens.Sixty; break;
case (int)Ones.Seven: result += Tens.Seventy; break;
case (int)Ones.Eight: result += Tens.Eighty; break;
case (int)Ones.Nine: result += Tens.Ninety; break; }
if (singles > 0) //include any singles { switch (singles) { case (int)Ones.One: result += "-" + Ones.One; break;
case (int)Ones.Two: result += "-" + Ones.Two; break;
case (int)Ones.Three: result += "-" + Ones.Three; break;
case (int)Ones.Four: result += "-" + Ones.Four; break;
case (int)Ones.Five: result += "-" + Ones.Five; break;
case (int)Ones.Six: result += "-" + Ones.Six; break;
case (int)Ones.Seven: result += "-" + Ones.Seven; break;
case (int)Ones.Eight: result += "-" + Ones.Eight; break;
case (int)Ones.Nine: result += "-" + Ones.Nine; break; } } } else //between 10 and 19 { switch (singles) { case (int)Ones.Zero: result += ****s.Ten; break;
case (int)Ones.One: result += ****s.Eleven; break;
case (int)Ones.Two: result += ****s.Twelve; break;
case (int)Ones.Three: result += ****s.Thir****; break;
case (int)Ones.Four: result += ****s.Four****; break;
case (int)Ones.Five: result += ****s.Fif****; break;
case (int)Ones.Six: result += ****s.Six****; break;
case (int)Ones.Seven: result += ****s.Seven****; break;
case (int)Ones.Eight: result += ****s.Eigh****; break;
case (int)Ones.Nine: result += ****s.Nine****; break; } } return result; }
private string BuildSingles(int singles, string tag) { string result = string.Empty;
switch (singles) { case (int)Ones.One: result = Ones.One + tag; break;
case (int)Ones.Two: result = Ones.Two + tag; break;
case (int)Ones.Three: result = Ones.Three + tag; break;
case (int)Ones.Four: result = Ones.Four + tag; break;
case (int)Ones.Five: result = Ones.Five + tag; break;
case (int)Ones.Six: result = Ones.Six + tag; break;
case (int)Ones.Seven: result = Ones.Seven + tag; break;
case (int)Ones.Eight: result = Ones.Eight + tag; break;
case (int)Ones.Nine: result = Ones.Nine + tag; break; }
return result; } } }
|