C# 将整数转换为书面数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert integers to written numbers
提问by GateKiller
Is there an efficient method of converting an integer into the written numbers, for example:
是否有一种将整数转换为书面数字的有效方法,例如:
string Written = IntegerToWritten(21);
would return "Twenty One".
将返回“二十一”。
Is there any way of doing this that doesn't involve a massive look-up table?
有没有什么方法可以不涉及大量查找表?
采纳答案by Wedge
This should work reasonably well:
这应该工作得相当好:
public static class HumanFriendlyInteger
{
static string[] ones = new string[] { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
static string[] teens = new string[] { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
static string[] tens = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
static string[] thousandsGroups = { "", " Thousand", " Million", " Billion" };
private static string FriendlyInteger(int n, string leftDigits, int thousands)
{
if (n == 0)
{
return leftDigits;
}
string friendlyInt = leftDigits;
if (friendlyInt.Length > 0)
{
friendlyInt += " ";
}
if (n < 10)
{
friendlyInt += ones[n];
}
else if (n < 20)
{
friendlyInt += teens[n - 10];
}
else if (n < 100)
{
friendlyInt += FriendlyInteger(n % 10, tens[n / 10 - 2], 0);
}
else if (n < 1000)
{
friendlyInt += FriendlyInteger(n % 100, (ones[n / 100] + " Hundred"), 0);
}
else
{
friendlyInt += FriendlyInteger(n % 1000, FriendlyInteger(n / 1000, "", thousands+1), 0);
if (n % 1000 == 0)
{
return friendlyInt;
}
}
return friendlyInt + thousandsGroups[thousands];
}
public static string IntegerToWritten(int n)
{
if (n == 0)
{
return "Zero";
}
else if (n < 0)
{
return "Negative " + IntegerToWritten(-n);
}
return FriendlyInteger(n, "", 0);
}
}
(Edited to fix a bug w/ million, billion, etc.)
(编辑以修复百万、十亿等的错误)
回答by lubos hasko
why massive lookup table?
为什么要大量查找表?
string GetWrittenInteger(int n)
{
string[] a = new string[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" }
string[] b = new string[] { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }
string[] c = new string[] {"Twenty", "Thirty", "Forty", "Sixty", "Seventy", "Eighty", "Ninety"};
string[] d = new string[] {"Hundred", "Thousand", "Million"}
string s = n.ToString();
for (int i = 0; i < s.Length; i++)
{
// logic (too lazy but you get the idea)
}
}
回答by Calanus
Justin Rogers has a "NumbersToEnglish" class which should do the job for you nicely!
贾斯汀·罗杰斯 (Justin Rogers) 有一个“NumbersToEnglish”课程,它应该可以很好地为您完成工作!
Initial posting.
http://weblogs.asp.net/justin_rogers/archive/2004/06/09/151675.aspx
初次发帖。
http://weblogs.asp.net/justin_rogers/archive/2004/06/09/151675.aspx
Finalized Source Code
http://weblogs.asp.net/justin_rogers/articles/151757.aspx
最终的源代码
http://weblogs.asp.net/justin_rogers/articles/151757.aspx
It does have a bit of an internal lookup table but I don't really know how you are going to be able to get away from that.
它确实有一些内部查找表,但我真的不知道您将如何摆脱它。
回答by Nick Masao
I use this code.It is VB code but you can easily translate it to C#. It works
我使用此代码。它是 VB 代码,但您可以轻松将其转换为 C#。有用
Function NumberToText(ByVal n As Integer) As String
Select Case n
Case 0
Return ""
Case 1 To 19
Dim arr() As String = {"One","Two","Three","Four","Five","Six","Seven", _
"Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen", _
"Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}
Return arr(n-1) & " "
Case 20 to 99
Dim arr() as String = {"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}
Return arr(n -2) & " " & NumberToText(n Mod 10)
Case 100 to 199
Return "One Hundred " & NumberToText(n Mod 100)
Case 200 to 999
Return NumberToText(n0) & "Hundreds " & NumberToText(n mod 100)
Case 1000 to 1999
Return "One Thousand " & NumberToText(n Mod 1000)
Case 2000 to 999999
Return NumberToText(n00) & "Thousands " & NumberToText(n Mod 1000)
Case 1000000 to 1999999
Return "One Million " & NumberToText(n Mod 1000000)
Case 1000000 to 999999999
Return NumberToText(n00000) & "Millions " & NumberToText(n Mod 1000000)
Case 1000000000 to 1999999999
Return "One Billion " & NumberTotext(n Mod 1000000000)
Case Else
Return NumberToText(n00000000) & "Billion " _
& NumberToText(n mod 1000000000)
End Select
End Function
Here is the code in c#
这是c#中的代码
public static string AmountInWords(double amount)
{
var n = (int)amount;
if (n == 0)
return "";
else if (n > 0 && n <= 19)
{
var arr = new string[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
return arr[n - 1] + " ";
}
else if (n >= 20 && n <= 99)
{
var arr = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
return arr[n / 10 - 2] + " " + AmountInWords(n % 10);
}
else if (n >= 100 && n <= 199)
{
return "One Hundred " + AmountInWords(n % 100);
}
else if (n >= 200 && n <= 999)
{
return AmountInWords(n / 100) + "Hundred " + AmountInWords(n % 100);
}
else if (n >= 1000 && n <= 1999)
{
return "One Thousand " + AmountInWords(n % 1000);
}
else if (n >= 2000 && n <= 999999)
{
return AmountInWords(n / 1000) + "Thousand " + AmountInWords(n % 1000);
}
else if (n >= 1000000 && n <= 1999999)
{
return "One Million " + AmountInWords(n % 1000000);
}
else if (n >= 1000000 && n <= 999999999)
{
return AmountInWords(n / 1000000) + "Million " + AmountInWords(n % 1000000);
}
else if (n >= 1000000000 && n <= 1999999999)
{
return "One Billion " + AmountInWords(n % 1000000000);
}
else
{
return AmountInWords(n / 1000000000) + "Billion " + AmountInWords(n % 1000000000);
}
}
回答by Karthik
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace tryingstartfror4digits
{
class Program
{
static void Main(string[] args)
{
Program pg = new Program();
Console.WriteLine("Enter ur number");
int num = Convert.ToInt32(Console.ReadLine());
if (num <= 19)
{
string g = pg.first(num);
Console.WriteLine("The number is " + g);
}
else if ((num >= 20) && (num <= 99))
{
if (num % 10 == 0)
{
string g = pg.second(num / 10);
Console.WriteLine("The number is " + g);
}
else
{
string g = pg.second(num / 10) + pg.first(num % 10);
Console.WriteLine("The number is " + g);
}
}
else if ((num >= 100) && (num <= 999))
{
int k = num % 100;
string g = pg.first(num / 100) +pg.third(0) + pg.second(k / 10)+pg.first(k%10);
Console.WriteLine("The number is " + g);
}
else if ((num >= 1000) && (num <= 19999))
{
int h = num % 1000;
int k = h % 100;
string g = pg.first(num / 1000) + "Thousand " + pg.first(h/ 100) + pg.third(k) + pg.second(k / 10) + pg.first(k % 10);
Console.WriteLine("The number is " + g);
}
Console.ReadLine();
}
public string first(int num)
{
string name;
if (num == 0)
{
name = " ";
}
else
{
string[] arr1 = new string[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" , "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
name = arr1[num - 1];
}
return name;
}
public string second(int num)
{
string name;
if ((num == 0)||(num==1))
{
name = " ";
}
else
{
string[] arr1 = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
name = arr1[num - 2];
}
return name;
}
public string third(int num)
{
string name ;
if (num == 0)
{
name = "";
}
else
{
string[] arr1 = new string[] { "Hundred" };
name = arr1[0];
}
return name;
}
}
}
this works fine from 1 to 19999 will update soon after i complete it
这在 1 到 19999 期间工作正常,我完成后很快就会更新
回答by Joe Meyer
Here is a C# Console Applicationthat will return whole numbers as well as decimals.
这是一个C# 控制台应用程序,它将返回整数和小数。
回答by Emre Guldogan
Just for Turkish representation of the class HumanFriendlyInteger(↑) (Türk?e, say? yaz? kar??l???):
仅用于类HumanFriendlyInteger(↑) (Türk?e, say? yaz? kar??l???) 的土耳其语表示:
public static class HumanFriendlyInteger
{
static string[] ones = new string[] { "", "Bir", "?ki", "ü?", "D?rt", "Be?", "Alt?", "Yedi", "Sekiz", "Dokuz" };
static string[] teens = new string[] { "On", "On Bir", "On ?ki", "On ü?", "On D?rt", "On Be?", "On Alt?", "On Yedi", "On Sekiz", "On Dokuz" };
static string[] tens = new string[] { "Yirmi", "Otuz", "K?rk", "Elli", "Altm??", "Yetmi?", "Seksen", "Doksan" };
static string[] thousandsGroups = { "", " Bin", " Milyon", " Milyar" };
private static string FriendlyInteger(int n, string leftDigits, int thousands)
{
if (n == 0)
{
return leftDigits;
}
string friendlyInt = leftDigits;
if (friendlyInt.Length > 0)
{
friendlyInt += " ";
}
if (n < 10)
friendlyInt += ones[n];
else if (n < 20)
friendlyInt += teens[n - 10];
else if (n < 100)
friendlyInt += FriendlyInteger(n % 10, tens[n / 10 - 2], 0);
else if (n < 1000)
friendlyInt += FriendlyInteger(n % 100, ((n / 100 == 1 ? "" : ones[n / 100] + " ") + "Yüz"), 0); // Yüz 1 ile ba?lang??ta "Bir" kelimesini Türk?e'de almaz.
else
friendlyInt += FriendlyInteger(n % 1000, FriendlyInteger(n / 1000, "", thousands + 1), 0);
return friendlyInt + thousandsGroups[thousands];
}
public static string IntegerToWritten(int n)
{
if (n == 0)
return "S?f?r";
else if (n < 0)
return "Eksi " + IntegerToWritten(-n);
return FriendlyInteger(n, "", 0);
}
回答by CleverPatrick
The accepted answer doesn't seem to work perfectly. It doesn't handle dashes in numbers like twenty-one, it doesn't put the word "and" in for numbers like "one hundred and one", and, well, it is recursive.
接受的答案似乎并不完美。它不处理像 21 这样的数字中的破折号,它不会将“和”这个词放在像“一百零一”这样的数字中,而且,它是递归的。
Here is my shot at the answer. It adds the "and" word intelligently, and hyphenates numbers appropriately. Let me know if any modifications are needed.
这是我对答案的看法。它巧妙地添加了“和”字,并适当地将数字连字符。让我知道是否需要任何修改。
Here is how to call it (obviously you will want to put this in a class somewhere):
下面是如何调用它(显然你想把它放在某个类的某个地方):
for (int i = int.MinValue+1; i < int.MaxValue; i++)
{
Console.WriteLine(ToWords(i));
}
Here is the code:
这是代码:
private static readonly string[] Ones = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
private static readonly string[] Teens =
{
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen"
};
private static readonly string[] Tens =
{
"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty",
"Ninety"
};
public static string ToWords(int number)
{
if (number == 0)
return "Zero";
var wordsList = new List<string>();
if (number < 0)
{
wordsList.Add("Negative");
number = Math.Abs(number);
}
if (number >= 1000000000 && number <= int.MaxValue) //billions
{
int billionsValue = number / 1000000000;
GetValuesUnder1000(billionsValue, wordsList);
wordsList.Add("Billion");
number -= billionsValue * 1000000000;
if (number > 0 && number < 10)
wordsList.Add("and");
}
if (number >= 1000000 && number < 1000000000) //millions
{
int millionsValue = number / 1000000;
GetValuesUnder1000(millionsValue, wordsList);
wordsList.Add("Million");
number -= millionsValue * 1000000;
if (number > 0 && number < 10)
wordsList.Add("and");
}
if (number >= 1000 && number < 1000000) //thousands
{
int thousandsValue = number/1000;
GetValuesUnder1000(thousandsValue, wordsList);
wordsList.Add("Thousand");
number -= thousandsValue * 1000;
if (number > 0 && number < 10)
wordsList.Add("and");
}
GetValuesUnder1000(number, wordsList);
return string.Join(" ", wordsList);
}
private static void GetValuesUnder1000(int number, List<string> wordsList)
{
while (number != 0)
{
if (number < 10)
{
wordsList.Add(Ones[number]);
number -= number;
}
else if (number < 20)
{
wordsList.Add(Teens[number - 10]);
number -= number;
}
else if (number < 100)
{
int tensValue = ((int) (number/10))*10;
int onesValue = number - tensValue;
if (onesValue == 0)
{
wordsList.Add(Tens[tensValue/10]);
}
else
{
wordsList.Add(Tens[tensValue/10] + "-" + Ones[onesValue]);
}
number -= tensValue;
number -= onesValue;
}
else if (number < 1000)
{
int hundredsValue = ((int) (number/100))*100;
wordsList.Add(Ones[hundredsValue/100]);
wordsList.Add("Hundred");
number -= hundredsValue;
if (number > 0)
wordsList.Add("and");
}
}
}
回答by Dhiraj D B
just get that string and convert with the like as string s=txtNumber.Text.Tostring(); int i=Convert.ToInt32(s.Tostring()); it will write only full integer value
只需获取该字符串并转换为字符串 s=txtNumber.Text.Tostring(); int i=Convert.ToInt32(s.Tostring()); 它只会写入完整的整数值
回答by SArifin
An extension of Nick Masao's answer for Bengali Numeric of same problem. Inital input of number is in Unicode string. Cheers!!
Nick Masao 对孟加拉数字相同问题的回答的扩展。数字的初始输入是 Unicode 字符串。干杯!!
string number = "????";
number = number.Replace("?", "0").Replace("?", "1").Replace("?", "2").Replace("?", "3").Replace("?", "4").Replace("?", "5").Replace("?", "6").Replace("?", "7").Replace("?", "8").Replace("?", "9");
double vtempdbl = Convert.ToDouble(number);
string amount = AmountInWords(vtempdbl);
private static string AmountInWords(double amount)
{
var n = (int)amount;
if (n == 0)
return " ";
else if (n > 0 && n <= 99)
{
var arr = new string[] { "??", "???", "???", "???", "????", "???", "???", "??", "???", "??", "????", "????", "???", "?????", "????", "????", "????", "????", "????", "???", "????", "????", "????", "??????", "?????", "???????", "?????", "????", "???????", "?????", "???????", "??????", "???????", "???????", "????????", "??????", "?????????", "???????", "????????", "??????", "????????", "?????????", "?????????", "?????????", "?????????", "????????", "?????????", "????????", "????????", "??????", "??????", "???????", "?????????", "???????", "????????", "?????????", "???????", "??????", "?????", "???", "???????", "???????", "???????", "???????", "???????", "???????", " ????????", "???????", "??????? ", "?????", "??????? ", "????????", "????????", "????????", "????????", "????????", "????????", "???????", "?????", "???", "?????", "??????", "??????", "??????", "??????", "??????", "??????", "?????", "???????", "?????", "????????", "?????????", "?????????", "?????????", "????????? ", "????????? ", "?????????", "????????", "?????????" };
return arr[n - 1] + " ";
}
else if (n >= 100 && n <= 199)
{
return AmountInWords(n / 100) + "?? ?? " + AmountInWords(n % 100);
}
else if (n >= 100 && n <= 999)
{
return AmountInWords(n / 100) + "?? " + AmountInWords(n % 100);
}
else if (n >= 1000 && n <= 1999)
{
return "?? ????? " + AmountInWords(n % 1000);
}
else if (n >= 1000 && n <= 99999)
{
return AmountInWords(n / 1000) + "????? " + AmountInWords(n % 1000);
}
else if (n >= 100000 && n <= 199999)
{
return "?? ??? " + AmountInWords(n % 100000);
}
else if (n >= 100000 && n <= 9999999)
{
return AmountInWords(n / 100000) + "??? " + AmountInWords(n % 100000);
}
else if (n >= 10000000 && n <= 19999999)
{
return "?? ???? " + AmountInWords(n % 10000000);
}
else
{
return AmountInWords(n / 10000000) + "???? " + AmountInWords(n % 10000000);
}
}