i found this code and edit it but some bug about number

wondery

Freshman
Joined
Sep 12, 2003
Messages
43
HELP! HELP! HELP ME PLEASE >> i found this code and edit it but some bug about number

i call it like this

C#:
double NetTotal = ((SubTotal-Advance)+TRate)+Vat;
lblNetTotal.Text = NetTotal.ToString("###,###,##0.00");
fs(NetTotal);

and this is fs function
C#:
public void fs(double allNum)
		{
		string firstNum=Convert.ToString(allNum);

			ArrayList al = new ArrayList();
			string[] parts = firstNum.Split('.');
			al.InsertRange(0,parts);
			int countArray = 0;

			foreach(string a in al) 
			{
				countArray +=1;
				if (countArray==1)
				{
					double NetFirst = Convert.ToDouble(a);
					lblToText.Text = IntegerToWords(NetFirst);
				}
				else
				{
					double NetSec = Convert.ToDouble(a);
					lblToText.Text = lblToText.Text + " and " + IntegerToWords(NetSec);
				}
			}

		}
and this is interger to word function
C#:
public string IntegerToWords(double inputNum) 
		{
			int dig1,dig2,dig3,level=0,lasttwo,threeDigits;

			string retval = ""; 
			string x = "";
			string[] ones={
							  "Zero",
							  "One",
							  "Two",
							  "Three",
							  "Four",
							  "Five",
							  "Six",
							  "Seven",
							  "Eight",
							  "Nine",
							  "Ten",
							  "Eleven",
							  "Twelve",
							  "Thirteen",
							  "Fourteen",
							  "Fifteen",
							  "Sixteen",
							  "Seventeen",
							  "Eighteen",
							  "Nineteen"
						  };
			string[] tens={
							  "Zero",
							  "Ten",
							  "Twenty",
							  "Thirty",
							  "Forty",
							  "Fifty",
							  "Sixty",
							  "Seventy",
							  "Eighty",
							  "Ninety"
						  };
			string[] thou={
							  "",
							  "Thousand",
							  "Million",
							  "Billion",
							  "Trillion",
							  "Quadrillion",
							  "Quintillion"
						  };

			bool isNegative=false;
			if (inputNum<0) 
			{
				isNegative=true;
				inputNum*=-1;
			}

			if (inputNum==0)
				return ("zero");

			string s=inputNum.ToString();

			while (s.Length>0) 
			{
				// Get the three rightmost characters
				x=(s.Length<3) ? s : s.Substring(s.Length - 3, 3);

				// Separate the three digits
				threeDigits=int.Parse(x);
				lasttwo=threeDigits % 100;
				dig1=threeDigits / 100;
				dig2=lasttwo / 10;
				dig3=(threeDigits % 10);

				// append a "thousand" where appropriate
				if (level>0 && dig1+dig2+dig3>0) 
				{
					retval=thou[level] + " " + retval;
					retval=retval.Trim();
				}

				// check that the last two digits is nonzero    
				if (lasttwo>0) 
				{
					if (lasttwo<20)  // if less than 20, use "ones" only 
						retval=ones[lasttwo] + " " + retval;
					else  // otherwise, use both "tens" and "ones" array
						retval=tens[dig2] + " " + ones[dig3] + " " + retval;
				}

				// if a hundreds part is there, translate it
				if (dig1>0)
					retval = ones[dig1] + " hundred " + retval;

				s=(s.Length - 3)>0 ? s.Substring(0,s.Length - 3) : "";
				level++;
			}

			while (retval.IndexOf("  ")>0)
				retval=retval.Replace("  "," ");

			retval=retval.Trim();

			if (isNegative)
				retval="negative " + retval;
			
			return (retval);
		}

problem is if number is 4,846.03 it will convert to Four Thousand Eight hundred Forty Six and Three and it should be Four Thousand Eight hundred Forty Six and Zero Three ,right ?

please help!!!
 
I guess it depends on what you want. I'd prefer "and three", not "and zero three". I didn't look at the IntegerToWords function, but I'm assuming it's converting the "03" string to a number, which is just 3 and then converting it to the word. You'd have to look at the function in detail to change it if that's what you want.

Now, if this were for decimal calculations, not money, I'd agree that might want it "and zero three". Then again, you'd probably want the word "cents" on the end and also say "and no cents" if the number were "1.00". Since you have the source code, you can modify it however you (or your clients) want.

Also, change that name from IntegerToWords to NumberToWords or something. I say "NumberToWords" instead of "DoubleToWords" (which would match the type) because you may want to overload this eventually to take other datatypes.

-Nerseus
 
Back
Top