如何使用c#将数字转换为texual格式 [英] how to convert numbers into texual format using c#
本文介绍了如何使用c#将数字转换为texual格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个Windows应用程序,我需要将数字转换为文本格式,就像有人输入6000然后它转换为六千。
请帮助我。
i am developing a windows application where i need to convert to numbers into text format like if someone enters 6000 then it is converted into six thousand.
please help me.
推荐答案
请参阅C#的这篇简短帖子。我对它进行了简单测试,结果非常好:
http://forums.exchangecore.com/topic/684-convert-number-to-words-c-console-application/
你只需复制两个代码使用它的功能:
See this short post for C#. I briefly tested it and it works very well:
http://forums.exchangecore.com/topic/684-convert-number-to-words-c-console-application/
You just need to copy the two functions to your code to use it:
static String NumWordsWrapper(double n)
{
string words = "";
double intPart;
double decPart = 0;
if (n == 0)
return "zero";
try
{
string[] splitter = n.ToString().Split('.');
intPart = double.Parse(splitter[0]);
decPart = double.Parse(splitter[1]);
}
catch
{
intPart = n;
}
words = NumWords(intPart);
if (decPart > 0)
{
if (words != "")
words += " and ";
int counter = decPart.ToString().Length;
switch (counter)
{
case 1: words += NumWords(decPart) + " tenths"; break;
case 2: words += NumWords(decPart) + " hundredths"; break;
case 3: words += NumWords(decPart) + " thousandths"; break;
case 4: words += NumWords(decPart) + " ten-thousandths"; break;
case 5: words += NumWords(decPart) + " hundred-thousandths"; break;
case 6: words += NumWords(decPart) + " millionths"; break;
case 7: words += NumWords(decPart) + " ten-millionths"; break;
}
}
return words;
}
static String NumWords(double n) //converts double to words
{
string[] numbersArr = new string[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
string[] tensArr = new string[] { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninty" };
string[] suffixesArr = new string[] { "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "Quattuordecillion", "Quindecillion", "Sexdecillion", "Septdecillion", "Octodecillion", "Novemdecillion", "Vigintillion" };
string words = "";
bool tens = false;
if (n < 0)
{
words += "negative ";
n *= -1;
}
int power = (suffixesArr.Length + 1) * 3;
while (power > 3)
{
double pow = Math.Pow(10, power);
if (n > pow)
{
if (n % Math.Pow(10, power) > 0)
{
words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1] + ", ";
}
else if (n % pow > 0)
{
words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1];
}
n %= pow;
}
power -= 3;
}
if (n >= 1000)
{
if (n % 1000 > 0) words += NumWords(Math.Floor(n / 1000)) + " thousand, ";
else words += NumWords(Math.Floor(n / 1000)) + " thousand";
n %= 1000;
}
if (0 <= n && n <= 999)
{
if ((int)n / 100 > 0)
{
words += NumWords(Math.Floor(n / 100)) + " hundred";
n %= 100;
}
if ((int)n / 10 > 1)
{
if (words != "")
words += " ";
words += tensArr[(int)n / 10 - 2];
tens = true;
n %= 10;
}
if (n < 20)
{
if (words != "" && tens == false)
words += " ";
words += (tens ? "-" + numbersArr[(int)n - 1] : numbersArr[(int)n - 1]);
n -= Math.Floor(n);
}
}
return words;
}
希望这对你有帮助。
Hope this will help you.
private void button1_Click(object sender, EventArgs e)
{
label1.Text = NoToWords(3500);
}
public static string NoToWords(int no)
{
if (no == 0)
return "zero";
if (no < 0)
return "minus " + NoToWords(Math.Abs(no));
string stringValue = "";
if ((no / 1000000) > 0)
{
stringValue += NoToWords(no / 1000000) + " million ";
no %= 1000000;
}
if ((no / 1000) > 0)
{
stringValue += NoToWords(no / 1000) + " thousand ";
no %= 1000;
}
if ((no / 100) > 0)
{
stringValue += NoToWords(no / 100) + " hundred ";
no %= 100;
}
if (no > 0)
{
if (stringValue != "")
stringValue += "and ";
var units = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
var tens = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
if (no < 20)
stringValue += units[no];
else
{
stringValue += tens[no / 10];
if ((no % 10) > 0)
stringValue += "-" + units[no % 10];
}
}
return stringValue;
}
谢谢!!!
Thanks!!!
hiii,
将对象定义为
AmountInWords obj = new AmountInWords();
obj.ConvertAmount(100)
和输出=一百
其中
AmountInWords.cs
hiii,
Define Object as
AmountInWords obj=new AmountInWords();
obj.ConvertAmount(100)
And Output is= One Hundred
where
AmountInWords.cs
class AmountInWords
{
private static String[] units = { "Zero", "One", "Two", "Three",
"Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
"Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen" };
private static String[] tens = { "", "", "Twenty", "Thirty", "Forty",
"Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
public String ConvertAmount(double amount)
{
try
{
int amount_int = (int)amount;
int amount_dec = (int)Math.Round((amount - (double)(amount_int)) * 100);
return convert(amount_int) + " point "
+ convert(amount_dec);
}
catch (Exception e)
{
// TODO: handle exception
}
return "";
}
public String convert(int i)
{
//
if (i < 20)
return units[i];
if (i < 100)
return tens[i / 10] + ((i % 10 > 0) ? " " + convert(i % 10) : "");
if (i < 1000)
return units[i / 100] + " Hundred"
+ ((i % 100 > 0) ? " and " + convert(i % 100) : "");
if (i < 100000)
return convert(i / 1000) + " Thousand "
+ ((i % 1000 > 0) ? " " + convert(i % 1000) : "");
if (i < 10000000)
return convert(i / 100000) + " Lakh "
+ ((i % 100000 > 0) ? " " + convert(i % 100000) : "");
return convert(i / 10000000) + " Crore "
+ ((i % 10000000 > 0) ? " " + convert(i % 10000000) : "");
}
}
这篇关于如何使用c#将数字转换为texual格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文