我有windows应用程序,我需要将在文本框中输入的数量转换为ruppes和paise格式的单词。
例如我的金额是2356.54
然后它应该显示two thousand three hundred fifty six ruppes and fifty four paise only
但是,我有代码将amt转换为单词但我无法显示paise
。我的代码包括参考目的。
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();
}
它不应该显示pasie,如果它有这样的数量2356.00
所以我试过很多方法来获得paise
但不成功。我试过ggogle但没有得到我想要的。
答案 0 :(得分:4)
您需要分隔十进制数,并在小数位前一位得到两个单独的值,在它后面得一位。例如在56.2中,你分别得到56和分别得到2,并为它们两个调用words()函数。你会得到两个字符串一个“五十六”和第二个“两个”。你可以加入这些字符串来说“五十六卢比2的paisas”。
答案 1 :(得分:0)
您需要对方法进行一些小的更改。请找到以下方法
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();
}
答案 2 :(得分:-4)
输入数字至999999999,输出将为单词。
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();
}
}