Convert sum in rupees and paise to word format in C #

I have a Windows application in which I need to convert the amount entered in a text box into words in ruppes and paise format. for example, My sum is 2356.54 , then it should display two thousand three hundred fifty six ruppes and fifty four paise only , but I have a code that converts amt to words, but I can not show paise . i am including my code for reference purpose.

 private void btntowords_Click(object sender, EventArgs e) { MessageBox.Show( words(Convert.ToInt32(textBox1.Text))); } public string words(int numbers) { int number = numbers; if (number == 0) return "Zero"; if (number == -2147483648) return "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight"; int[] num = new int[4]; int first = 0; int u, h, t; System.Text.StringBuilder sb = new System.Text.StringBuilder(); if (number < 0) { sb.Append("Minus "); number = -number; } string[] words0 = {"" ,"One ", "Two ", "Three ", "Four ", "Five " ,"Six ", "Seven ", "Eight ", "Nine "}; string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "}; string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ","Eighty ", "Ninety "}; string[] words3 = { "Thousand ", "Lakh ", "Crore " }; num[0] = number % 1000; // units num[1] = number / 1000; num[2] = number / 100000; num[1] = num[1] - 100 * num[2]; // thousands num[3] = number / 10000000; // crores num[2] = num[2] - 100 * num[3]; // lakhs for (int i = 3; i > 0; i--) { if (num[i] != 0) { first = i; break; } } for (int i = first; i >= 0; i--) { if (num[i] == 0) continue; u = num[i] % 10; // ones t = num[i] / 10; h = num[i] / 100; // hundreds t = t - 10 * h; // tens if (h > 0) sb.Append(words0[h] + "Hundred "); if (u > 0 || t > 0) { if (h > 0 || i == 0) sb.Append("and "); if (t == 0) sb.Append(words0[u]); else if (t == 1) sb.Append(words1[u]); else sb.Append(words2[t - 2] + words0[u]); } if (i != 0) sb.Append(words3[i - 1]); } return sb.ToString().TrimEnd(); } 

It should not show pasie if it has a quantity like this 2356.00 so I tried in many ways to get paise , but it won't work. I tried ggogle but didn’t get exactly what I want.

+7
source share
3 answers

You need to divide the decimal number and get two separate values: one before the decimal and one after it. For example, in 56.2 you get 56 separately and 2 separately and call the words () function for both of them. You will get two lines: “Fifty-six” and the second “two”. You can join these lines to say “Fifty-six rupees 2 paise”.

+4
source

You need a little change in your method. Please find below method

 public string words(double? numbers, Boolean paisaconversion = false) { var pointindex = numbers.ToString().IndexOf("."); var paisaamt = 0; if (pointindex > 0) paisaamt = Convert.ToInt32( numbers.ToString().Substring(pointindex + 1, 2)); int number = Convert.ToInt32( numbers); if (number == 0) return "Zero"; if (number == -2147483648) return "Minus Two Hundred and Fourteen Crore Seventy Four Lakh Eighty Three Thousand Six Hundred and Forty Eight"; int[] num = new int[4]; int first = 0; int u, h, t; System.Text.StringBuilder sb = new System.Text.StringBuilder(); if (number < 0) { sb.Append("Minus "); number = -number; } string[] words0 = {"" ,"One ", "Two ", "Three ", "Four ","Five " ,"Six ", "Seven ", "Eight ", "Nine "}; string[] words1 = {"Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ", "Nineteen "}; string[] words2 = {"Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ","Seventy ","Eighty ", "Ninety "}; string[] words3 = { "Thousand ", "Lakh ", "Crore " }; num[0] = number % 1000; // units num[1] = number / 1000; num[2] = number / 100000; num[1] = num[1] - 100 * num[2]; // thousands num[3] = number / 10000000; // crores num[2] = num[2] - 100 * num[3]; // lakhs for (int i = 3; i > 0; i--) { if (num[i] != 0) { first = i; break; } } for (int i = first; i >= 0; i--) { if (num[i] == 0) continue; u = num[i] % 10; // ones t = num[i] / 10; h = num[i] / 100; // hundreds t = t - 10 * h; // tens if (h > 0) sb.Append(words0[h] + "Hundred "); if (u > 0 || t > 0) { if (h > 0 || i == 0) sb.Append("and "); if (t == 0) sb.Append(words0[u]); else if (t == 1) sb.Append(words1[u]); else sb.Append(words2[t - 2] + words0[u]); } if (i != 0) sb.Append(words3[i - 1]); } if (paisaamt == 0 && paisaconversion == false) { sb.Append("ruppes only"); } else if (paisaamt > 0) { var paisatext = words(paisaamt, true); sb.AppendFormat("rupees {0} paise only", paisatext); } return sb.ToString().TrimEnd(); } 
0
source

Give input as a number up to 999999999, the result will be in words.

 class A { string[] words0 = { "Zero ", "One ", "Two ", "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", "Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", "Seventeen ", "Eighteen ", "Nineteen ", "Twenty " }; string[] words2 = { "Zero ", "Ten ", "Twenty ", "Thirty ", "Forty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", "Ninety ", "Hundred " }; string[] words3 = { "Hundred ", "Thousand ", "Lakh ", "Crore " }; int[] numbers = new int[] { 0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; string numstr; string words = ""; int tempNum; int temp = 0; public void ConvertToRupee(int number) { numstr = number.ToString(); words = ""; tempNum = number; temp = 0; while (numstr != "0" && numstr.Length != 0) { switch (numstr.Length) { case 1: words += words0[tempNum]; numstr = ""; break; case 2: if (tempNum <= 20) { words += words0[tempNum]; numstr = ""; } else { temp = tempNum / numbers[2]; words += words2[temp]; tempNum = tempNum % numbers[2]; numstr = tempNum.ToString(); } break; case 3: Method1(3, "Hundred "); break; case 4: Method1(4, "Thousand "); break; case 5: Method2(4, "Thousand "); break; case 6: Method1(6, "Lakh "); break; case 7: Method2(6, "Lakh "); break; case 8: Method1(8, "Crore "); break; case 9: Method2(8, "Crore "); break; default: break; } } words += "Rupees Only "; Console.WriteLine(number.ToString() + " : " + words); } private void Method1(int n, string wo) { temp = tempNum / numbers[n]; words += words0[temp] + wo; tempNum = tempNum % numbers[n]; numstr = tempNum.ToString(); } private void Method2(int n, string wo) { temp = tempNum / numbers[n]; if (temp == 10) words += words0[temp] + wo; else if (temp <= 20) words += words0[temp] + wo; else { int twoDig = temp / numbers[2]; int digit = temp % numbers[2]; words += words2[twoDig] + words0[digit] + wo; } tempNum = tempNum % numbers[n]; numstr = tempNum.ToString(); } } 
-4
source

Source: https://habr.com/ru/post/907745/


All Articles