本文介绍了如何将123.12数字等十进制数转换为单词卢比并使用C#进行调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 private void textWeight_TextChanged(object sender, EventArgs e) { try { //textWeight.Text = Double.Parse(textWeight.Text).ToString("N3"); string s = "select Max(SrNo)+1 From TounchData"; OleDbCommand csm = new OleDbCommand(s, connection); connection.Open(); csm.ExecuteNonQuery(); OleDbDataReader dd = csm.ExecuteReader(); while (dd.Read()) { int n = dd.GetInt32(0); textSrNo.Text = n.ToString(); } connection.Close(); //string NumericValue = textTounch.Text; //string getString = changeToWords(NumericValue); //LabelNoToWord.Text = getString; } catch (Exception ex) { MessageBox.Show("Error" + ex); //textTounch.Text = ""; //LabelNoToWord.Text = ""; } { LabelNoToWord.Text = "0"; } if (textWeight.Text != "") LabelNoToWord.Text = (AmountInWords(Convert.ToInt32(textWeight.Text))); } public string AmountInWords(decimal Num) { string returnValue; //I have created this function for converting amount in indian rupees (INR). //You can manipulate as you wish like decimal setting, Doller (any currency) Prefix. string strNum; string strNumDec; string StrWord; strNum = Num.ToString(); if (strNum.IndexOf(".") + 1 != 0) { strNumDec = strNum.Substring(strNum.IndexOf(".") + 2 - 1); if (strNumDec.Length == 1) { strNumDec = strNumDec + "0"; } if (strNumDec.Length > 2) { strNumDec = strNumDec.Substring(0, 2); } strNum = strNum.Substring(0, strNum.IndexOf(".") + 0); StrWord = ((double.Parse(strNum) == 1) ? " Rupee " : " Rupees ") + NumToWord((decimal)(double.Parse(strNum))) + ((double.Parse(strNumDec) > 0) ? (" and Paise" + cWord3((decimal)(double.Parse(strNumDec)))) : ""); } else { StrWord = ((double.Parse(strNum) == 1) ? " Rupee " : " Rupees ") + NumToWord((decimal)(double.Parse(strNum))); } returnValue = StrWord + " Only"; return returnValue; } static public string NumToWord(decimal Num) { string returnValue; //I divided this function in two part. //1. Three or less digit number. //2. more than three digit number. string strNum; string StrWord; strNum = Num.ToString(); if (strNum.Length <= 3) { StrWord = cWord3((decimal)(double.Parse(strNum))); } else { StrWord = cWordG3((decimal)(double.Parse(strNum.Substring(0, strNum.Length - 3)))) + " " + cWord3((decimal)(double.Parse(strNum.Substring(strNum.Length - 2 - 1)))); } returnValue = StrWord; return returnValue; } static public string cWordG3(decimal Num) { string returnValue; //2. more than three digit number. string strNum = ""; string StrWord = ""; string readNum = ""; strNum = Num.ToString(); if (strNum.Length % 2 != 0) { readNum = System.Convert.ToString(double.Parse(strNum.Substring(0, 1))); if (readNum != "0") { StrWord = retWord(decimal.Parse(readNum)); readNum = System.Convert.ToString(double.Parse("1" + strReplicate("0", strNum.Length - 1) + "000")); StrWord = StrWord + " " + retWord(decimal.Parse(readNum)); } strNum = strNum.Substring(1); } while (!System.Convert.ToBoolean(strNum.Length == 0)) { readNum = System.Convert.ToString(double.Parse(strNum.Substring(0, 2))); if (readNum != "0") { StrWord = StrWord + " " + cWord3(decimal.Parse(readNum)); readNum = System.Convert.ToString(double.Parse("1" + strReplicate("0", strNum.Length - 2) + "000")); StrWord = StrWord + " " + retWord(decimal.Parse(readNum)); } strNum = strNum.Substring(2); } returnValue = StrWord; return returnValue; } static public string cWord3(decimal Num) { string returnValue; //1. Three or less digit number. string strNum = ""; string StrWord = ""; string readNum = ""; if (Num < 0) { Num = Num * -1; } strNum = Num.ToString(); if (strNum.Length == 3) { readNum = System.Convert.ToString(double.Parse(strNum.Substring(0, 1))); StrWord = retWord(decimal.Parse(readNum)) + " Hundred"; strNum = strNum.Substring(1, strNum.Length - 1); } if (strNum.Length <= 2) { if (double.Parse(strNum) >= 0 && double.Parse(strNum) <= 20) { StrWord = StrWord + " " + retWord((decimal)(double.Parse(strNum))); } else { StrWord = StrWord + " " + retWord((decimal)(System.Convert.ToDouble(strNum.Substring(0, 1) + "0"))) + " " + retWord((decimal)(double.Parse(strNum.Substring(1, 1)))); } } strNum = Num.ToString(); returnValue = StrWord; return returnValue; } static public string retWord(decimal Num) { string returnValue; //This two dimensional array store the primary word convertion of number. returnValue = ""; object[,] ArrWordList = new object[,] { { 0, "" }, { 1, "One" }, { 2, "Two" }, { 3, "Three" }, { 4, "Four" }, { 5, "Five" }, { 6, "Six" }, { 7, "Seven" }, { 8, "Eight" }, { 9, "Nine" }, { 10, "Ten" }, { 11, "Eleven" }, { 12, "Twelve" }, { 13, "Thirteen" }, { 14, "Fourteen" }, { 15, "Fifteen" }, { 16, "Sixteen" }, { 17, "Seventeen" }, { 18, "Eighteen" }, { 19, "Nineteen" }, { 20, "Twenty" }, { 30, "Thirty" }, { 40, "Forty" }, { 50, "Fifty" }, { 60, "Sixty" }, { 70, "Seventy" }, { 80, "Eighty" }, { 90, "Ninety" }, { 100, "Hundred" }, { 1000, "Thousand" }, { 100000, "Lakh" }, { 10000000, "Crore" } }; int i; for (i = 0; i <= (ArrWordList.Length - 1); i++) { if (Num == System.Convert.ToDecimal(ArrWordList[i, 0])) { returnValue = (string)(ArrWordList[i, 1]); break; } } return returnValue; } static public string strReplicate(string str, int intD) { string returnValue; //This fucntion padded "0" after the number to evaluate hundred, thousand and on.... //using this function you can replicate any Charactor with given string. int i; returnValue = ""; for (i = 1; i <= intD; i++) { returnValue = returnValue + str; } return returnValue; } 我的尝试: 我创建了这个函数用于转换印度卢比(INR)的金额,所以你可以帮助我。 但是我想把金额换成卢比并用c#oledb中的单词格式换算服务器What I have tried:I have created this function for converting amount in Indian Rupees (INR) so can you help me.but i want to convert amount to rupees and paise in words format in c# oledb Server推荐答案 private void button2_Click(object sender, EventArgs e) { GenerateWordsinRs(); } private void GenerateWordsinRs() { decimal numberrs = Convert.ToDecimal(txtNumber.Text); CultureInfo ci = new CultureInfo("en-IN"); string aaa = String.Format("{0:#,##0.##}", numberrs); aaa = aaa + " " + ci.NumberFormat.CurrencySymbol.ToString(); label6.Text = aaa; string input = txtNumber.Text; string a = ""; string b = ""; // take decimal part of input. convert it to word. add it at the end of method. string decimals = ""; if (input.Contains(".")) { decimals = input.Substring(input.IndexOf(".") + 1); // remove decimal part from input input = input.Remove(input.IndexOf(".")); } string strWords = NUMBERTOWORDS.NumbersToWords(Convert.ToInt32(input)); if (!txtNumber.Text.Contains(".")) { a = strWords + " Rupees Only"; } else { a = strWords + " Rupees"; } if (decimals.Length > 0) { // if there is any decimal part convert it to words and add it to strWords. string strwords2 = NUMBERTOWORDS.NumbersToWords(Convert.ToInt32(decimals)); b = " and " + strwords2 + " Paisa Only "; } label7.Text = a + b; } //NUMBERTOWORDS.NumbersToWords(Convert.ToInt32(input)); //NUMBERTOWORDS IS A CLASS//NUMBERTOWORDS CLASSusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace rounfoffmath{ class NUMBERSTOWORDS { public static string NumbersToWords(int inputNumber) { int inputNo = inputNumber; if (inputNo == 0) return "Zero"; int[] numbers = new int[4]; int first = 0; int u, h, t; System.Text.StringBuilder sb = new System.Text.StringBuilder(); if (inputNo < 0) { sb.Append("Minus "); inputNo = -inputNo; } 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 " }; numbers[0] = inputNo % 1000; // units numbers[1] = inputNo / 1000; numbers[2] = inputNo / 100000; numbers[1] = numbers[1] - 100 * numbers[2]; // thousands numbers[3] = inputNo / 10000000; // crores numbers[2] = numbers[2] - 100 * numbers[3]; // lakhs for (int i = 3; i > 0; i--) { if (numbers[i] != 0) { first = i; break; } } for (int i = first; i >= 0; i--) { if (numbers[i] == 0) continue; u = numbers[i] % 10; // ones t = numbers[i] / 10; h = numbers[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(""); 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(); } }}// enJOY tHIS code, have fun, try it to convert amount into indian currency and words into indian formats. :) developed by shaanpublic conversion(string amount) {double m = Convert.ToInt64(Math.Floor(Convert.ToDouble(amount))); double l = Convert.ToDouble(amount); double j = ( l - m ) * 100; var beforefloating = ConvertNumbertoWords(Convert.ToInt64(m)); var afterfloating = ConvertNumbertoWords(Convert.ToInt64(j)); var Content = beforefloating + ' ' + " RUPEES" + ' ' + afterfloating + ' ' + " PAISE only";} main code of this Program main code of this Programpublic string ConvertNumbertoWords(long number) { if (number == 0) return "ZERO"; if (number < 0) return "minus " + ConvertNumbertoWords(Math.Abs(number)); string words = ""; if ((number / 1000000) > 0) { words += ConvertNumbertoWords(number / 100000) + " LAKES "; number %= 1000000; } if ((number / 1000) > 0) { words += ConvertNumbertoWords(number / 1000) + " THOUSAND "; number %= 1000; } if ((number / 100) > 0) { words += ConvertNumbertoWords(number / 100) + " HUNDRED "; number %= 100; } //if ((number / 10) > 0) //{ // words += ConvertNumbertoWords(number / 10) + " RUPEES "; // number %= 10; //} if (number > 0) { if (words != "") words += "AND "; var unitsMap = new[] { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN" }; var tensMap = new[] { "ZERO", "TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY" }; if (number < 20) words += unitsMap[number]; else { words += tensMap[number / 10]; if ((number % 10) > 0) words += " " + unitsMap[number % 10]; } } return words; } like this two time calling the same function can generate words for decimal numberslike this two time calling the same function can generate words for decimal numbers 这篇关于如何将123.12数字等十进制数转换为单词卢比并使用C#进行调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-31 09:13