在C#中将数字转换为单词

时间:2019-09-21 16:12:27

标签: c#

我知道互联网上有很多食谱,但是我想自己掌握。

使用简单的方法,我只想访问数组并从用户输入中获取=数字的“ WORD”

例如

  • 输入33
  • 33

所以我必须将33除以10

  • 获取第一个数字并乘以10
  • 拿剩下的钱
  • 从十数组中获取“三十”
  • 和单位中的“树”

如何将信息与数组进行比较并了解重要信息? 要循环吗?

int number;
int i = 0;

String[] units = new String[] { "one", "two", "three" };
String[] tens = new String[] { "twenty", "thirty", "forty" };

Console.WriteLine("Please enter a number");
number = Convert.ToInt32(Console.ReadLine());

if (number < 20)
{
    Console.WriteLine("The number is");
    Console.WriteLine(units[]);
}
else if (number > 20)
{
    Console.WriteLine("The number is");
    Console.WriteLine(tens[]);
}

1 个答案:

答案 0 :(得分:2)

尝试以下代码:

using System;
using System.Collections.Generic;

namespace ConsoleApp2
{
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");

        NumberToWordConverter nc = new NumberToWordConverter();


        Console.WriteLine(nc.ConvertNumberToWord(0));
        Console.WriteLine(nc.ConvertNumberToWord(5));
        Console.WriteLine(nc.ConvertNumberToWord(17));
        Console.WriteLine(nc.ConvertNumberToWord(37));
        Console.WriteLine(nc.ConvertNumberToWord(147));
        Console.WriteLine(nc.ConvertNumberToWord(252));
        Console.WriteLine(nc.ConvertNumberToWord(489));
        Console.WriteLine(nc.ConvertNumberToWord(900));
        Console.WriteLine(nc.ConvertNumberToWord(950));
        Console.WriteLine(nc.ConvertNumberToWord(999));


        Console.ReadLine();
    }
}


public class NumberToWordConverter
{
    private Dictionary<long, string> numWordDict = new Dictionary<long, string>();


    public NumberToWordConverter()
    {
        numWordDict.Add(0, "zero");
        numWordDict.Add(1, "one");
        numWordDict.Add(2, "two");
        numWordDict.Add(3, "three");
        numWordDict.Add(4, "four");
        numWordDict.Add(5, "five");
        numWordDict.Add(6, "six");
        numWordDict.Add(7, "seven");
        numWordDict.Add(8, "eight");
        numWordDict.Add(9, "nine");
        numWordDict.Add(10, "ten");
        numWordDict.Add(11, "eleven");
        numWordDict.Add(12, "twelve");
        numWordDict.Add(13, "thirteen");
        numWordDict.Add(14, "fourteen");
        numWordDict.Add(15, "fifteen");
        numWordDict.Add(16, "sixteen");
        numWordDict.Add(17, "seventeen");
        numWordDict.Add(18, "eightteen");
        numWordDict.Add(19, "nineteen");
        numWordDict.Add(20, "twenty");
        numWordDict.Add(30, "thirty");
        numWordDict.Add(40, "forty");
        numWordDict.Add(50, "fifty");
        numWordDict.Add(60, "sixty");
        numWordDict.Add(70, "seventy");
        numWordDict.Add(80, "eighty");
        numWordDict.Add(90, "ninety");
        numWordDict.Add(100, "hundred");

    }

    /// <summary>
    /// Only goes up to 900 but you can modify this code to make it go up higher
    /// </summary>
    /// <param name="number"></param>
    /// <returns></returns>
    public string ConvertNumberToWord(long number)
    {
        string nstring = string.Empty;

        if (number == 0)
        {
            return numWordDict[number];
        }

        if(number < 20)
        {
            return numWordDict[number];
        }

        long hundreds = number / 100;
        number -= hundreds * 100;
        long tens = number / 10;
        number -= tens * 10;
        long ones = number;


        if (hundreds > 0)
        {
            nstring = numWordDict[hundreds] + " " + numWordDict[100];
        }

        if (tens > 0)
        {
            if (!string.IsNullOrWhiteSpace(nstring))
            {
                nstring += " and ";
            }

            nstring += numWordDict[tens * 10];
        }

        if (ones > 0)
        {
            if (!string.IsNullOrWhiteSpace(nstring))
            {
                nstring += " ";
            }

            nstring += numWordDict[ones];

        }

        return nstring;
    }
}

}

您可以毫不费力地将其扩展到超过1000。 我相信在这里使用字典而不是列表会更好。它的速度很快,而且您出错的可能性也较小。