整数を書かれた数値に変換する効率的な方法はありますか。たとえば、次のようになります。
string Written = IntegerToWritten(21);
"Twenty One" を返します。
大規模なルックアップ テーブルを使用せずにこれを行う方法はありますか?
これはかなりうまくいくはずです:
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);
}
}
(100 万、10 億などのバグを修正するために編集)
Humanizer という便利なライブラリを使用しています。
https://github.com/Humanizr/Humanizer
複数の文化をサポートし、数字だけでなく日付も変換します。使い方は非常に簡単です。
使用方法は次のとおりです。
int someNumber = 543;
var culture = System.Globalization.CultureInfo("en-US");
var result = someNumber.ToWords(culture); // 543 -> five hundred forty-three
そして出来上がり!
このコードを使用します。これは 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\10 -2) & " " & NumberToText(n Mod 10)
Case 100 to 199
Return "One Hundred " & NumberToText(n Mod 100)
Case 200 to 999
Return NumberToText(n\100) & "Hundreds " & NumberToText(n mod 100)
Case 1000 to 1999
Return "One Thousand " & NumberToText(n Mod 1000)
Case 2000 to 999999
Return NumberToText(n\1000) & "Thousands " & NumberToText(n Mod 1000)
Case 1000000 to 1999999
Return "One Million " & NumberToText(n Mod 1000000)
Case 1000000 to 999999999
Return NumberToText(n\1000000) & "Millions " & NumberToText(n Mod 1000000)
Case 1000000000 to 1999999999
Return "One Billion " & NumberTotext(n Mod 1000000000)
Case Else
Return NumberToText(n\1000000000) & "Billion " _
& NumberToText(n mod 1000000000)
End Select
End Function
これが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);
}
}
なぜ大規模なルックアップテーブルなのですか?
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)
}
}
受け入れられた答えは完全に機能していないようです。これは、21 のような数字のダッシュを処理しません。また、「100 と 1」のような数字に「and」という単語を入れません。また、再帰的です。
これが私の答えです。「and」という単語をインテリジェントに追加し、数字を適切にハイフネーションします。変更が必要な場合はお知らせください。
これを呼び出す方法は次のとおりです(明らかに、これをどこかのクラスに入れたいと思うでしょう):
for (int i = int.MinValue+1; i < int.MaxValue; i++)
{
Console.WriteLine(ToWords(i));
}
コードは次のとおりです。
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");
}
}
}
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;
}
}
}
これは 1 から 19999 まで正常に動作します。完了後すぐに更新されます
以下は、小数だけでなく整数も返すC# コンソール アプリケーションです。
その文字列を取得して、string s=txtNumber.Text.Tostring(); のように変換するだけです。int i=Convert.ToInt32(s.Tostring()); 完全な整数値のみを書き込みます