-1
我正在嘗試使一個號碼翻譯最多可以處理任何數字
999999999並將其轉換爲字符串表示形式。號碼翻譯最多999999999到單詞
輸入= 999999999 輸出=九百九十900萬九百九十九千九百九十九
一個更好的方式來處理這個問題的任何幫助將不勝感激。
完全披露我已經使用了這個以前的解決方案很多想法 。
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();
}
}