我需要将一个整数值转换为它的英文单词等效形式(即4 =>“four”,1879 =>“one thousand, eight hundred seventy-nine”)在.NET中(具体来说是3.5)。我想知道是否有任何内置于.NET框架中的东西可以进行这样的转换。似乎足够有用,应该属于其中。我还没有能够找到做这个工作的任何东西。如果它没有包含在框架中,有没有比数字/位置特定的查找更优雅的想法?
输入的数字为字符串 s。
const string input = "1023";
string[] placement = { "thousand", "hundred", "ten", "" };
string[] numbersToLetters = { "", "one", "two", "tre", "four", "five", "six", "seven", "eight", "nine" };
for (int i = 0; i < input.Length; i++)
{
int digits = input[i] != '0' ? (placement.Length - input.Length) + i : 3;
int result = int.Parse(input[i].ToString());
var type = placement[digits];
var number = numbersToLetters[result];
Console.WriteLine(number + type);
}
我不知道有任何内置的东西。你只需要进行一些数字解析和替换。
我在网上找到了一些例子:
http://www.dotnetspider.com/resources/2743-Code-Convert-numbers-word.aspx http://www.codeproject.com/KB/cs/codesamples.aspx
没有直接的函数可以为您转换和创建单词形式。您将需要编写程序,基本上您将不得不硬编码所有数字的值,即1一个,2两个,...,9九个。然后,您还需要注意十位、百位、千位,然后您将需要编写逻辑来提取数字并相应地附加单词。