gpt4 book ai didi

c# - 单词的新数字

转载 作者:行者123 更新时间:2023-11-30 23:13:11 25 4
gpt4 key购买 nike

我正在尝试制作一个将数字更改为单词的程序,我知道它存在并由 Converting numbers in to words 回答

但我想做的是例如我有一个输入 114321程序首先进入循环处理 321three hundred twenty one然后是 114但前缀为千 one hundred fourteen thousand所以整体输出就像one hundred fourteen thousand three hundred twenty one .我这样试过,但它错了,我什至没有关闭

public static string NumberToWords(int number)
{
var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
var hundredsMap = new[] { "", "Thousand", "Million" };
string words = "";
string snumber = number.ToString();
int tempInt = number;
int counter;

for (counter = 0;counter <= (snumber.Length/3); counter++)
{
if (snumber.Length > 3)
{
tempInt = Convert.ToInt32(snumber.Substring(snumber.Length - 3));
}

if ((tempInt / 100) > 0)
{
words += NumberToWords(tempInt / 100) + " hundred ";
tempInt %= 100;
}

if (tempInt != 0)
{
if (tempInt < 20)
{
words += unitsMap[tempInt];
}
else
{
words += tensMap[tempInt / 10];
if ((tempInt % 10) > 0)
{
words += " " + unitsMap[tempInt % 10];
}
}
}
words += hundredsMap[counter];
}
return words;
}

最佳答案

以最少的更改修复代码的主要技巧是,您应该按从右到左的顺序分别为每个组构建字符串,然后按从左到右的顺序将它们添加到字符串中。通过这种方式,您还可以相对轻松地处理额外空间的需求(我的意思是,当您有“1000300”时,您会得到“one_million_three_hundreds”,尽管整个“thousands”组都丢失了,但所有单词之间只有一个空格)。

    public static string NumberToWords(int number)
{
if (number == 0)
return "zero";

var unitsMap = new[] { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
var tensMap = new[] { "", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
var hundredsMap = new[] { "", " Thousand", " Million" };

string fullString = "";
bool negative = (number < 0);
int rest = Math.Abs(number);


for (int counter = 0; rest > 0; counter++)
{
string groupWords = "";
int currentGroup = rest % 1000;
rest /= 1000;

int hundreds = currentGroup / 100;
if (hundreds > 0)
{
groupWords += unitsMap[hundreds] + " hundred";
currentGroup %= 100;
}

if (currentGroup != 0)
{
if (groupWords.Length > 0)
groupWords += " ";
if (currentGroup < 20)
{
groupWords += unitsMap[currentGroup];
}
else
{
groupWords += tensMap[currentGroup / 10];
if ((currentGroup % 10) > 0)
{
groupWords += " " + unitsMap[currentGroup % 10];
}
}
}

// handle case such as just "one million"
if (groupWords.Length > 0)
{
groupWords += hundredsMap[counter];
// handle case such as just "one million"
if (fullString.Length == 0)
fullString = groupWords;
else
fullString = groupWords + " " + fullString;
}
}
return negative ? "minus " + fullString : fullString;
}

另请注意,您根本不需要 snumber(更不用说它的用法中的一个错误,您总是得到每个组的最后 3 位数字)

关于c# - 单词的新数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43748700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com