我正在尝试制作一个能够处理任何数字的数字翻译器 999999999并将其转换为字符串表示。
输入= 999999999 输出=九百九十九百万九十九九九,九百九十九
任何有关处理此问题的更好方法的帮助都将受到赞赏。
完全披露我使用了很多想法 从以前的解决方案。
converting numbers in to words C#
public partial class MainWindow : Window
{
int inputNumber;
public MainWindow()
{
InitializeComponent();
}
private void Translate_Click(object sender, RoutedEventArgs e)
{
if (int.TryParse(txtInput.Text, out inputNumber) )
{
TranslateNumber();
}
}
private void TranslateNumber()
{
var unitsMap = new[] { " ", " One ", " Two ", " Three ", " Four ", " Five ", " Six ", " Seven ", " Eight ", " Nine ", " Ten ", " Eleven ", " Twelve ", " Thirteen ", " Fourteen ", " Fifteen ", " Sixteen ", " Seventeen ", " Eighteen ", " Nineteen " };
var tensMap = new[] { " ", " Ten ", " Twenty ", " Thirty ", " Forty ", " Fifty ", " Sixty ", " Seventy ", " Eighty ", " Ninety " };
if (inputNumber == 0)
{
tBlkOutput.Text += "zero";
}
if ((inputNumber / 100000000 ) > 0)
{
// needs a number betwwen 1 and 9
tBlkOutput.Text += unitsMap[inputNumber / 100000000] + " Hundred Million ";
inputNumber %= 100000000;
}
if ((inputNumber / 1000000) > 0 )
{
// need to be able to list between 1 million and 99 million
if ((inputNumber / 10000000) > 0)
{
tBlkOutput.Text += tensMap[inputNumber / 10000000];
inputNumber %= 10000000;
}
if ((inputNumber / 1000000) > 0)
{
tBlkOutput.Text += unitsMap[inputNumber / 1000000];
inputNumber %= 1000000;
}
tBlkOutput.Text += " Million ";
}
if ((inputNumber / 100000) > 0)
{
// needs a number betwwen 1 and 9
tBlkOutput.Text += unitsMap[inputNumber / 1000000] + " Hundred Thousand ";
inputNumber %= 1000000;
}
if ((inputNumber / 10000) > 0)
{
if ((inputNumber / 100000) > 0)
{
tBlkOutput.Text += tensMap[inputNumber / 10000];
inputNumber %= 10000;
}
if ((inputNumber / 1000) > 0)
{
tBlkOutput.Text += unitsMap[inputNumber / 1000];
inputNumber %= 1000;
}
tBlkOutput.Text += " Thousand ";
}
if ((inputNumber / 100) > 0)
{
tBlkOutput.Text += unitsMap[inputNumber / 100] + " Hundred and ";
inputNumber %= 100;
}
if ((inputNumber / 10 ) > 0)
{
tBlkOutput.Text += tensMap[inputNumber / 10];
inputNumber %= 10;
}
if (inputNumber > 0)
{
tBlkOutput.Text += unitsMap[inputNumber];
}
}
private void Reset_Click(object sender, RoutedEventArgs e)
{
tBlkOutput.Text = "";
inputNumber = 0;
txtInput.Text = "";
txtInput.Focus();
}
}
答案 0 :(得分:0)
我弄清楚我哪里出错了,把一些if语句放在他们需要的地方,现在它大部分都有效。
public partial class MainWindow : Window
{
int inputNumber;
public MainWindow()
{
InitializeComponent();
}
private void Translate_Click(object sender, RoutedEventArgs e)
{
if (int.TryParse(txtInput.Text, out inputNumber) )
{
TranslateNumber();
}
}
private void TranslateNumber()
{
var unitsMap = new[] { " ", " One ", " Two ", " Three ", " Four ", " Five ", " Six ", " Seven ", " Eight ", " Nine ", " Ten ", " Eleven ", " Twelve ", " Thirteen ", " Fourteen ", " Fifteen ", " Sixteen ", " Seventeen ", " Eighteen ", " Nineteen " };
var tensMap = new[] { " ", " Ten ", " Twenty ", " Thirty ", " Forty ", " Fifty ", " Sixty ", " Seventy ", " Eighty ", " Ninety " };
if (inputNumber == 0)
{
tBlkOutput.Text += "zero";
}
if ((inputNumber / 1000000) > 0 )
{
if ((inputNumber / 100000000) > 0)
{
// needs a number betwwen 1 and 9
tBlkOutput.Text += unitsMap[inputNumber / 100000000] + " Hundred and ";
inputNumber %= 100000000;
}
// need to be able to list between 1 million and 99 million
if ((inputNumber / 10000000) > 0)
{
tBlkOutput.Text += tensMap[inputNumber / 10000000];
inputNumber %= 10000000;
}
if ((inputNumber / 1000000) > 0)
{
tBlkOutput.Text += unitsMap[inputNumber / 1000000];
inputNumber %= 1000000;
}
tBlkOutput.Text += " Million ";
}
if ((inputNumber / 1000) > 0)
{
if ((inputNumber / 100000) > 0)
{
// needs a number betwwen 1 and 9
tBlkOutput.Text += unitsMap[inputNumber / 100000] + " Hundred and ";
inputNumber %= 100000;
}
if ((inputNumber / 10000) > 0)
{
tBlkOutput.Text += tensMap[inputNumber / 10000];
inputNumber %= 10000;
}
if ((inputNumber / 1000) > 0)
{
tBlkOutput.Text += unitsMap[inputNumber / 1000];
inputNumber %= 1000;
}
tBlkOutput.Text += " Thousand ";
}
if ((inputNumber / 100) > 0)
{
tBlkOutput.Text += unitsMap[inputNumber / 100] + " Hundred and ";
inputNumber %= 100;
}
if ((inputNumber / 10 ) > 0)
{
tBlkOutput.Text += tensMap[inputNumber / 10];
inputNumber %= 10;
}
if (inputNumber > 0)
{
tBlkOutput.Text += unitsMap[inputNumber];
}
}
private void Reset_Click(object sender, RoutedEventArgs e)
{
tBlkOutput.Text = "";
inputNumber = 0;
txtInput.Text = "";
txtInput.Focus();
}
}
答案 1 :(得分:0)
试试这个 C# 代码。
创建一个类并随意命名。就我而言,JayClass 然后编写如下所示的自定义可视化基本函数。
public class JayClass
{
public static object Choose(double index,params object[] choice)
{
if (index < 1 || index > choice.Length)
{
return " ";
}
else
{
return choice[Convert.ToInt32(--index)];
}
}
public static string Mid(string input, int start, int length)
{
string temp = input.Substring(start-1,length);
return temp;
}
public static string Mid(string input, int length)
{
string temp = input.Substring(length);
return temp;
}
public static string Left(string input , int length)
{
string temp = input.Substring(0,length);
return temp;
}
public static string Right(string input, int length)
{
string temp = input.Substring(input.Length-length, length);
return temp;
}
public static string Format(object Exprs, string style = "")
{
string temp = String.Format("{0:"+style+"}",Exprs);
return temp;
}
}
创建另一个类并命名您想要的任何名称,在我的情况下为 Trillion 并在类中编写以下方法。是的,“万亿”,因为它可以将数字从一到万亿转换为单词。
public class Trillion{
public static string GET_Trillion(double value)
{
string[] M = new string[49], L = new string[49], K = new string[100], Place = new string[51];
string PN, Results;
// ONES IN WORDS
M[1] = "one";
M[2] = "two";
M[3] = "three";
M[4] = "four";
M[5] = "five";
M[6] = "six";
M[7] = "seven";
M[8] = "eight";
M[9] = "nine";
// TENS IN WORDS
L[10] = "ten";
L[11] = "eleven";
L[12] = "twelve";
L[13] = "thirteen";
L[14] = "fourteen";
L[15] = "fifteen";
L[16] = "sixteen";
L[17] = "seventeen";
L[18] = "eighteen";
L[19] = "nineteen";
K[20] = "twenty";
K[30] = "thirty";
K[40] = "forty";
K[50] = "fifty";
K[60] = "sixty";
K[70] = "seventy";
K[80] = "eighty";
K[90] = "ninety";
PN = "and ";
// placement
Place[2] = " hundred ";
Place[3] = " thousand ";
Place[6] = " million ";
Place[9] = " billion ";
Place[12] = " trillion ";
if (value.ToString().Length > 15)
{
throw new Exception("OutOfRangeException\nMessage: Please input a value not greater than 15 digits.");
}
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 14, 1)) != 1)
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 15, 1))+1, "", M[1], M[2], M[3], M[4], M[5], M[6], M[7], M[8], M[9]);
}
else
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 15, 1)) + 1, L[10], L[11], L[12], L[13], L[14], L[15], L[16], L[17], L[18], L[19]);
}
if (Conversions.ToInteger(JayClass.Mid(JayClass.Format(value, "000000000000000"), 14, 1)) > 1 && Conversions.ToInteger(JayClass.Mid(JayClass.Format(value, "000000000000000"), 15, 1)) > 0)
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 14, 1))+1, "", "", K[20], K[30], K[40], K[50], K[60], K[70], K[80], K[90]) + "-" + Results;
}
else
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 14, 1))+1, "", "", K[20], K[30], K[40], K[50], K[60], K[70], K[80], K[90]) + Results;
}
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 13, 1)) == 0)
{
}
else if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 14, 1)) == 0 && Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 15, 1)) == 0)
{
Results = Place[2];
}
else
{
Results = Place[2] + PN + Results;
}
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 13, 1))+1, "", M[1], M[2], M[3], M[4], M[5], M[6], M[7], M[8], M[9]) + Results;
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 10, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 11, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 12, 1)) == 0)
{
}
else if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 13, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 14, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 15, 1)) == 0 || Convert.ToDouble( JayClass.Mid(JayClass.Format(value, "000000000000000"), 13, 1)) != 0)
{
Results = Place[3] + Results;
}
else
{
Results = Place[3] + PN + Results;
}
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 11, 1)) != 1)
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 12, 1))+1, "", M[1], M[2], M[3], M[4], M[5], M[6], M[7], M[8], M[9]) + Results;
}
else
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 12, 1)) + 1,L[10], L[11], L[12], L[13], L[14], L[15], L[16], L[17], L[18], L[19]) + Results;
}
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 11, 1)) > 1 && Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 12, 1)) > 0)
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 11, 1))+1, "", "", K[20], K[30], K[40], K[50], K[60], K[70], K[80], K[90]) + "-" + Results;
}
else
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 11, 1))+1, "", "", K[20], K[30], K[40], K[50], K[60], K[70], K[80], K[90]) + Results;
}
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 10, 1)) == 0)
{
}
else if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 11, 1)) == 0 && Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 12, 1)) == 0)
{
Results = Place[2] + Results;
}
else
{
Results = Place[2] + PN + Results;
}
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 10, 1))+1, "", M[1], M[2], M[3], M[4], M[5], M[6], M[7], M[8], M[9]) + Results;
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 7, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 8, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 9, 1) )== 0)
{
}
else if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 10, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 11, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 12, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 13, 1)) == 0 && Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 14, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 15, 1)) > 0)
{
Results = Place[6] + PN + Results;
}
else
{
Results = Place[6] + Results;
}
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 8, 1)) != 1)
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 9, 1))+1, "", M[1], M[2], M[3], M[4], M[5], M[6], M[7], M[8], M[9]) + Results;
}
else
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 9, 1)) + 1, L[10], L[11], L[12], L[13], L[14], L[15], L[16], L[17], L[18], L[19]) + Results;
}
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 8, 1)) > 1 && Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 9, 1)) > 0)
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 8, 1))+1, "", "", K[20], K[30], K[40], K[50], K[60], K[70], K[80], K[90]) + "-" + Results;
}
else
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 8, 1))+1, "", "", K[20], K[30], K[40], K[50], K[60], K[70], K[80], K[90]) + Results;
}
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 7, 1)) == 0)
{
}
else if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 8, 1)) == 0 && Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 9, 1) )== 0)
{
Results = Place[2] + Results;
}
else
{
Results = Place[2] + PN + Results;
}
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 7, 1))+1, "", M[1], M[2], M[3], M[4], M[5], M[6], M[7], M[8], M[9]) + Results;
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 4, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 5, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 6, 1)) == 0)
{
}
else if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 7, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 8, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 9, 1) +
JayClass.Mid(JayClass.Format(value, "000000000000000"), 10, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 11, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 12, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 13, 1)) == 0 && Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 14, 1) + JayClass.Right(JayClass.Format(value, "000000000000000"), 1)) > 0)
{
Results = Place[9] + PN + Results;
}
else
{
Results = Place[9] + Results;
}
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 5, 1)) != 1)
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 6, 1))+1, "", M[1], M[2], M[3], M[4], M[5], M[6], M[7], M[8], M[9]) + Results;
}
else
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 6, 1)) + 1, L[10], L[11], L[12], L[13], L[14], L[15], L[16], L[17], L[18], L[19]) + Results;
}
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 5, 1)) > 1 && Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 6, 1)) > 0)
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 5, 1))+1, "", "", K[20], K[30], K[40], K[50], K[60], K[70], K[80], K[90]) + "-" + Results;
}
else
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 5, 1))+1, "", "", K[20], K[30], K[40], K[50], K[60], K[70], K[80], K[90]) + Results;
}
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 4, 1)) == 0)
{
}
else if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 5, 1)) == 0 && Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 6, 1)) == 0)
{
Results = Place[2] + Results;
}
else
{
Results = Place[2] + PN + Results;
}
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 4, 1))+1, "", M[1], M[2], M[3], M[4], M[5], M[6], M[7], M[8], M[9]) + Results;
if (Convert.ToDouble(JayClass.Left(JayClass.Format(value, "000000000000000"), 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 2, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 3, 1)) == 0)
{
}
else if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 4, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 5, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 6, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 7, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 8, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 9, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 10, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 11, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 12, 1) + JayClass.Mid(JayClass.Format(value, "000000000000000"), 13, 1)) == 0 && Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 14, 1) + JayClass.Right(JayClass.Format(value, "000000000000000"), 1))> 0)
{
Results = Place[12] + PN + Results;
}
else
{
Results = Place[12] + Results;
}
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 2, 1)) != 1)
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 3, 1))+1, "", M[1], M[2], M[3], M[4], M[5], M[6], M[7], M[8], M[9]) + Results;
}
else
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 3, 1))+1, L[10], L[11], L[12], L[13], L[14], L[15], L[16], L[17], L[18], L[19]) + Results;
}
if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 2, 1)) > 1 && Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 3, 1)) > 0)
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 2, 1))+1, "", "", K[20], K[30], K[40], K[50], K[60], K[70], K[80], K[90]) + "-" + Results;
}
else
{
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 2, 1))+1, "", "", K[20], K[30], K[40], K[50], K[60], K[70], K[80], K[90]) + Results;
}
if (Convert.ToDouble(JayClass.Left(JayClass.Format(value, "000000000000000"), 1)) == 0)
{
}
else if (Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 2, 1)) == 0 && Convert.ToDouble(JayClass.Mid(JayClass.Format(value, "000000000000000"), 3, 1)) == 0)
{
Results = Place[2] + Results;
}
else
{
Results = Place[2] + PN + Results;
}
Results = (string)JayClass.Choose(Convert.ToDouble(JayClass.Left(JayClass.Format(value, "000000000000000"), 1))+1, "", M[1], M[2], M[3], M[4], M[5], M[6], M[7], M[8], M[9]) + Results;
return Results;
}
}
假设您输入这个数字,“9,999,999,999,999” 它将被翻译成完全像这样的词“九亿九千九百九九九九九十”和99999”
使用它,稍后感谢我。