是否有一种在C#中拼出int的开箱即用方法?例如,如果我有:
int a = 53;
我想打印:
"fifty three"
不
"53"
如果没有,是否有人有任何关于如何做到这一点的例子?
谢谢!
答案 0 :(得分:9)
你必须自己编写代码。如果不得不猜测,我会说它不在框架中,因为它几乎不可能本地化(除了数字名称之外还有更多内容:单词顺序,连字符规则等)。
这带回了回忆,因为这是我大学第一次编程课程的作业,所以你用英语写一篇也不会太难。
答案 1 :(得分:8)
以下是将号码转换为的代码 词语的
using System; namespace WpfApplication1 { public class NumberToEnglish { public String changeNumericToWords(double numb) { String num = numb.ToString(); return changeToWords(num, false); } public String changeCurrencyToWords(String numb) { return changeToWords(numb, true); } public String changeNumericToWords(String numb) { return changeToWords(numb, false); } public String changeCurrencyToWords(double numb) { return changeToWords(numb.ToString(), true); } private String changeToWords(String numb, bool isCurrency) { String val = "", wholeNo = numb, points = "", andStr = "", pointStr=""; String endStr = (isCurrency) ? ("Only") : (""); try { int decimalPlace = numb.IndexOf("."); if (decimalPlace > 0) { wholeNo = numb.Substring(0, decimalPlace); points = numb.Substring(decimalPlace+1); if (Convert.ToInt32(points) > 0) { andStr = (isCurrency)?("and"):("point");// just to separate whole numbers from > points/cents endStr = (isCurrency) ? ("Cents "+endStr) : (""); pointStr = translateCents(points); } } val = String.Format("{0} {1}{2} {3}",translateWholeNumber(wholeNo).Trim(),andStr,pointStr,endStr); } catch { ;} return val; } private String translateWholeNumber(String number) { string word = ""; try { bool beginsZero = false;//tests for 0XX bool isDone = false;//test if already translated double dblAmt = (Convert.ToDouble(number)); //if ((dblAmt > 0) && number.StartsWith("0")) if (dblAmt > 0) {//test for zero or digit zero in a nuemric beginsZero = number.StartsWith("0"); int numDigits = number.Length; int pos = 0;//store digit grouping String place = "";//digit grouping name:hundres,thousand,etc... switch (numDigits) { case 1://ones' range word = ones(number); isDone = true; break; case 2://tens' range word = tens(number); isDone = true; break; case 3://hundreds' range pos = (numDigits % 3) + 1; place = " Hundred "; break; case 4://thousands' range case 5: case 6: pos = (numDigits % 4) + 1; place = " Thousand "; break; case 7://millions' range case 8: case 9: pos = (numDigits % 7) + 1; place = " Million "; break; case 10://Billions's range pos = (numDigits % 10) + 1; place = " Billion "; break; //add extra case options for anything above Billion... default: isDone = true; break; } if (!isDone) {//if transalation is not done, continue...(Recursion comes in now!!) word = translateWholeNumber(number.Substring(0, pos)) + place + translateWholeNumber(number.Substring(pos)); //check for trailing zeros if (beginsZero) word = " and " + word.Trim(); } //ignore digit grouping names if (word.Trim().Equals(place.Trim())) word = ""; } } catch { ;} return word.Trim(); } private String tens(String digit) { int digt = Convert.ToInt32(digit); String name = null; switch (digt) { case 10: name = "Ten"; break; case 11: name = "Eleven"; break; case 12: name = "Twelve"; break; case 13: name = "Thirteen"; break; case 14: name = "Fourteen"; break; case 15: name = "Fifteen"; break; case 16: name = "Sixteen"; break; case 17: name = "Seventeen"; break; case 18: name = "Eighteen"; break; case 19: name = "Nineteen"; break; case 20: name = "Twenty"; break; case 30: name = "Thirty"; break; case 40: name = "Fourty"; break; case 50: name = "Fifty"; break; case 60: name = "Sixty"; break; case 70: name = "Seventy"; break; case 80: name = "Eighty"; break; case 90: name = "Ninety"; break; default: if (digt > 0) { name = tens(digit.Substring(0, 1) + "0") + "" + ones(digit.Substring(1)); } break; } return name; } private String ones(String digit) { int digt = Convert.ToInt32(digit); String name = ""; switch (digt) { case 1: name = "One"; break; case 2: name = "Two"; break; case 3: name = "Three"; break; case 4: name = "Four"; break; case 5: name = "Five"; break; case 6: name = "Six"; break; case 7: name = "Seven"; break; case 8: name = "Eight"; break; case 9: name = "Nine"; break; } return name; } private String translateCents(String cents) { String cts = "", digit = "", engOne = ""; for (int i = 0; i < cents.Length; i++) { digit = cents[i].ToString(); if (digit.Equals("0")) { engOne = "Zero"; } else { engOne = ones(digit); } cts += " " + engOne; } return cts; } } }
在项目中创建一个类文件, 将此代码复制到您的类文件中。 将命名空间更改为您的项目 命名空间。
创建该类的obj 像这样
NumberToEnglish objnumber = new NumberToEnglish();
和
objnumber.changeNumericToWords(100);
使用该功能,您将获得号码 进入单词。
来源:http://social.msdn.microsoft.com/Forums/en/wpf/thread/42b5bb54-dfd6-4c5b-8d51-82e5fc29f8e8 作者:Hiran Repakula
他使用了实例方法,但你可以制作这些方法static
。
答案 2 :(得分:6)
这是另一种方式,只是为了踢。
public static class NumericSpelling
{
private const long Quadrillion = Trillion * 1000;
private const long Trillion = Billion * 1000;
private const long Billion = Million * 1000;
private const long Million = Thousand * 1000;
private const long Thousand = Hundred * 10;
private const long Hundred = 100;
public static string ToVerbal(this int value) { return ToVerbal((long)value); }
public static string ToVerbal(this long value)
{
if (value == 0) return "zero";
if (value < 0)
{
return "negative " + ToVerbal(Math.Abs(value));
}
System.Text.StringBuilder builder = new StringBuilder();
int unit = 0;
if (value >= Quadrillion)
{
unit = (int)(value / Quadrillion);
value -= unit * Quadrillion;
builder.AppendFormat("{0}{1} quadrillion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
}
if (value >= Trillion)
{
unit = (int)(value / Trillion);
value -= unit * Trillion;
builder.AppendFormat("{0}{1} trillion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
}
if (value >= Billion)
{
unit = (int)(value / Billion);
value -= unit * Billion;
builder.AppendFormat("{0}{1} billion", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
}
if (value >= Million)
{
unit = (int)(value / Million);
value -= unit * Million;
builder.AppendFormat("{0}{1} million", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
}
if (value >= Thousand)
{
unit = (int)(value / Thousand);
value -= unit * Thousand;
builder.AppendFormat("{0}{1} thousand", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
}
if (value >= Hundred)
{
unit = (int)(value / Hundred);
value -= unit * Hundred;
builder.AppendFormat("{0}{1} hundred", builder.Length > 0 ? ", " : string.Empty, ToVerbal(unit));
}
if (builder.Length > 0 && value > 0) builder.AppendFormat(" and");
if (value >= 90)
{
value -= 90;
builder.AppendFormat("{0}ninety", builder.Length > 0 ? " " : string.Empty);
}
if (value >= 80)
{
value -= 80;
builder.AppendFormat("{0}eighty", builder.Length > 0 ? " " : string.Empty);
}
if (value >= 70)
{
value -= 70;
builder.AppendFormat("{0}seventy", builder.Length > 0 ? " " : string.Empty);
}
if (value >= 60)
{
value -= 60;
builder.AppendFormat("{0}sixty", builder.Length > 0 ? " " : string.Empty);
}
if (value >= 50)
{
value -= 50;
builder.AppendFormat("{0}fifty", builder.Length > 0 ? " " : string.Empty);
}
if (value >= 40)
{
value -= 40;
builder.AppendFormat("{0}forty", builder.Length > 0 ? " " : string.Empty);
}
if (value >= 30)
{
value -= 30;
builder.AppendFormat("{0}thirty", builder.Length > 0 ? " " : string.Empty);
}
if (value >= 20)
{
value -= 20;
builder.AppendFormat("{0}twenty", builder.Length > 0 ? " " : string.Empty);
}
if (value == 19) builder.AppendFormat("{0}nineteen", builder.Length > 0 ? " " : string.Empty);
if (value == 18) builder.AppendFormat("{0}eighteen", builder.Length > 0 ? " " : string.Empty);
if (value == 17) builder.AppendFormat("{0}seventeen", builder.Length > 0 ? " " : string.Empty);
if (value == 16) builder.AppendFormat("{0}sixteen", builder.Length > 0 ? " " : string.Empty);
if (value == 15) builder.AppendFormat("{0}fifteen", builder.Length > 0 ? " " : string.Empty);
if (value == 14) builder.AppendFormat("{0}fourteen", builder.Length > 0 ? " " : string.Empty);
if (value == 13) builder.AppendFormat("{0}thirteen", builder.Length > 0 ? " " : string.Empty);
if (value == 12) builder.AppendFormat("{0}twelve", builder.Length > 0 ? " " : string.Empty);
if (value == 11) builder.AppendFormat("{0}eleven", builder.Length > 0 ? " " : string.Empty);
if (value == 10) builder.AppendFormat("{0}ten", builder.Length > 0 ? " " : string.Empty);
if (value == 9) builder.AppendFormat("{0}nine", builder.Length > 0 ? " " : string.Empty);
if (value == 8) builder.AppendFormat("{0}eight", builder.Length > 0 ? " " : string.Empty);
if (value == 7) builder.AppendFormat("{0}seven", builder.Length > 0 ? " " : string.Empty);
if (value == 6) builder.AppendFormat("{0}six", builder.Length > 0 ? " " : string.Empty);
if (value == 5) builder.AppendFormat("{0}five", builder.Length > 0 ? " " : string.Empty);
if (value == 4) builder.AppendFormat("{0}four", builder.Length > 0 ? " " : string.Empty);
if (value == 3) builder.AppendFormat("{0}three", builder.Length > 0 ? " " : string.Empty);
if (value == 2) builder.AppendFormat("{0}two", builder.Length > 0 ? " " : string.Empty);
if (value == 1) builder.AppendFormat("{0}one", builder.Length > 0 ? " " : string.Empty);
return builder.ToString();
}
}
输入:
int first = 10447;
long second = 10576749323475;
int third = 0;
int fourth = -1095;
int fifth = 100;
int sixth = 102;
int seventh = 10004;
int eight = 100025;
Console.WriteLine(first.ToVerbal());
Console.WriteLine(second.ToVerbal());
Console.WriteLine(third.ToVerbal());
Console.WriteLine(fourth.ToVerbal());
Console.WriteLine(fifth.ToVerbal());
Console.WriteLine(sixth.ToVerbal());
Console.WriteLine(seventh.ToVerbal());
Console.WriteLine(eight.ToVerbal());
输出:
ten thousand, four hundred and forty seven
ten trillion, five hundred and seventy six billion, seven hundred and forty nine million, three hundred and twenty three thousand, four hundred and seventy five
zero
negative one thousand and ninety five
one hundred
one hundred and two
ten thousand and four
one hundred thousand and twenty five
答案 3 :(得分:4)
如果一切都失败了,请问比尔盖茨和他的工作人员。这应该让你开始
答案 4 :(得分:3)
答案 5 :(得分:0)
是的,有办法做到这一点。 International Components for Unicode项目有一个RuleBasedNumberFormatter,带有&#34;拼出&#34;选项。它甚至支持完全本地化。
唯一的障碍是它仅在C,C ++和Java中可用。 icu.net项目中有一项计划(适用于.NET Framework和.NET Standard 1.6),但此功能和许多其他功能尚未移植。但是,贡献可以很好地解决这个问题。
还有a tool应该能够在ICU4C库周围自动生成一个C#包装器库,但我还没有尝试过。
Other options可用于其他编程语言。