将包括美分在内的所有数字转换为单词
本文关键字:数字 转换 单词 包括美 | 更新日期: 2023-09-27 18:00:34
我的代码只转换(1-999)中的完整数字。我需要转换所有的数字,如果一个数字(例如:25648666258)包含要转换为单词的美分(例如:1928.25)。下面是我的代码。有人能帮我解决这个问题吗。
private void amt_txt_KeyUp(object sender, KeyEventArgs e)
{
string[] Ones = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen" };
string[] Tens = { "Ten", "Twenty", "Thirty", "Fourty", "Fift", "Sixty", "Seventy", "Eighty", "Ninty" };
int no = int.Parse(amt_txt.Text);
string strWords = "";
if (no > 999 && no < 10000)
{
int i = no / 1000;
strWords = strWords + Ones[i - 1] + " Thousand ";
no = no % 1000;
}
if (no > 99 && no < 1000)
{
int i = no / 100;
strWords = strWords + Ones[i - 1] + " Hundred ";
no = no % 100;
}
if (no > 19 && no < 100)
{
int i = no / 10;
strWords = strWords + Tens[i - 1] + " ";
no = no % 10;
}
if (no > 0 && no < 20)
{
strWords = strWords + Ones[no - 1];
}
cnv_txt.Text = strWords;
}
这是您需要的转换器。使用方法可以降低代码的复杂性。
这个算法把数字转换成这样的单词。
输入:"1234567"
-
获取最后3位数字。"567"
-
将其转换为Words。五百六十七
-
应用其分隔符。五百六十七(最后三位数字没有分隔符)
-
重复:获取最后3个数字"234"
-
将其转换为Words。二百三十四
-
应用其分隔符。贰拾叁万肆仟
-
附加到Resault。贰拾叁万肆仟伍佰陆拾柒
-
重复:获取最后3位数字。"1"(只剩下一位数字)
-
将其转换为Words。一个
-
应用其分隔符。一百万
-
附加到Resault。壹佰贰拾叁万肆仟伍佰陆拾柒
完成。
我已经对代码进行了评论,希望它能有所帮助。
如果你不理解某些部分,就要求解释。
同样,对于小数,我们将它们分开,转换为单词。最后我们将它们添加到最后的结果中。
private static void Main(string[] args)
{
string input = "123466265.123";
// 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("."));
}
// Convert input into words. save it into strWords
string strWords = GetWords(input);
if (decimals.Length > 0)
{
// if there is any decimal part convert it to words and add it to strWords.
strWords += " Point " + GetWords(decimals);
}
Console.WriteLine(strWords);
}
private static string GetWords(string input)
{
// these are seperators for each 3 digit in numbers. you can add more if you want convert beigger numbers.
string[] seperators = { "", " Thousand ", " Million ", " Billion " };
// Counter is indexer for seperators. each 3 digit converted this will count.
int i = 0;
string strWords = "";
while (input.Length > 0)
{
// get the 3 last numbers from input and store it. if there is not 3 numbers just use take it.
string _3digits = input.Length < 3 ? input : input.Substring(input.Length - 3);
// remove the 3 last digits from input. if there is not 3 numbers just remove it.
input = input.Length < 3 ? "" : input.Remove(input.Length - 3);
int no = int.Parse(_3digits);
// Convert 3 digit number into words.
_3digits = GetWord(no);
// apply the seperator.
_3digits += seperators[i];
// since we are getting numbers from right to left then we must append resault to strWords like this.
strWords = _3digits + strWords;
// 3 digits converted. count and go for next 3 digits
i++;
}
return strWords;
}
// your method just to convert 3digit number into words.
private static string GetWord(int no)
{
string[] Ones =
{
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
"Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen"
};
string[] Tens = {"Ten", "Twenty", "Thirty", "Fourty", "Fift", "Sixty", "Seventy", "Eighty", "Ninty"};
string word = "";
if (no > 99 && no < 1000)
{
int i = no/100;
word = word + Ones[i - 1] + " Hundred ";
no = no%100;
}
if (no > 19 && no < 100)
{
int i = no/10;
word = word + Tens[i - 1] + " ";
no = no%10;
}
if (no > 0 && no < 20)
{
word = word + Ones[no - 1];
}
return word;
}