如何将整数转换为其语言表示?

时间:2009-02-16 19:35:36

标签: c# .net .net-3.5

我可以使用库或类/函数将整数转换为它的口头表示吗?

示例输入:

  

4,567,788`

示例输出:

  

四百万,五十六万七千七百八十八

14 个答案:

答案 0 :(得分:44)

目前,最好,最强大的库绝对是Humanizer。它是开源的,可以作为nuget使用:

Console.WriteLine(4567788.ToWords()); // => four million five hundred and sixty-seven thousand seven hundred and eighty-eight

它还提供了广泛的工具,可以解决每个应用程序在stringenumDateTimeTimeSpan等方面遇到的小问题,并支持许多不同的语言。

Console.WriteLine(4567788.ToOrdinalWords().Underscore().Hyphenate().ApplyCase(LetterCasing.AllCaps)); // => FOUR-MILLION-FIVE-HUNDRED-AND-SIXTY-SEVEN-THOUSAND-SEVEN-HUNDRED-AND-EIGHTY-EIGHTH

答案 1 :(得分:28)

如果您使用以下代码: converting numbers in to words C# 你需要十进制数字,这是如何做到的:

public string DecimalToWords(decimal number)
{
    if (number == 0)
        return "zero";

    if (number < 0)
        return "minus " + DecimalToWords(Math.Abs(number));

    string words = "";

    int intPortion = (int)number;
    decimal fraction = (number - intPortion)*100;
    int decPortion = (int)fraction;

    words = NumericToWords(intPortion);
    if (decPortion > 0)
    {
        words += " and ";
        words += NumericToWords(decPortion);
    }
    return words;
}

答案 2 :(得分:9)

完全递归版本:

private static string[] ones = {
    "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", 
    "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
};

private static string[] tens = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

private static string[] thous = { "hundred", "thousand", "million", "billion", "trillion", "quadrillion" };

private static string fmt_negative = "negative {0}";
private static string fmt_dollars_and_cents = "{0} dollars and {1} cents";
private static string fmt_tens_ones = "{0}-{1}"; // e.g. for twenty-one, thirty-two etc. You might want to use an en-dash or em-dash instead of a hyphen.
private static string fmt_large_small = "{0} {1}"; // stitches together the large and small part of a number, like "{three thousand} {five hundred forty two}"
private static string fmt_amount_scale = "{0} {1}"; // adds the scale to the number, e.g. "{three} {million}";

public static string ToWords(decimal number) {
    if (number < 0)
        return string.format(fmt_negative, ToWords(Math.Abs(number)));

    int intPortion = (int)number;
    int decPortion = (int)((number - intPortion) * (decimal) 100);

    return string.Format(fmt_dollars_and_cents, ToWords(intPortion), ToWords(decPortion));
}

private static string ToWords(int number, string appendScale = "") {
    string numString = "";
    // if the number is less than one hundred, then we're mostly just pulling out constants from the ones and tens dictionaries
    if (number < 100) {
        if (number < 20)
            numString = ones[number];
        else {
            numString = tens[number / 10];
            if ((number % 10) > 0)
                numString = string.Format(fmt_tens_ones, numString, ones[number % 10]);
        }
    } else {
        int pow = 0; // we'll divide the number by pow to figure out the next chunk
        string powStr = ""; // powStr will be the scale that we append to the string e.g. "hundred", "thousand", etc.

        if (number < 1000) { // number is between 100 and 1000
            pow = 100; // so we'll be dividing by one hundred
            powStr = thous[0]; // and appending the string "hundred"
        } else { // find the scale of the number
            // log will be 1, 2, 3 for 1_000, 1_000_000, 1_000_000_000, etc.
            int log = (int)Math.Log(number, 1000);
            // pow will be 1_000, 1_000_000, 1_000_000_000 etc.
            pow = (int)Math.Pow(1000, log);
            // powStr will be thousand, million, billion etc.
            powStr = thous[log];
        }

        // we take the quotient and the remainder after dividing by pow, and call ToWords on each to handle cases like "{five thousand} {thirty two}" (curly brackets added for emphasis)
        numString = string.Format(fmt_large_small, ToWords(number / pow, powStr), ToWords(number % pow)).Trim();
    }

    // and after all of this, if we were passed in a scale from above, we append it to the current number "{five} {thousand}"
    return string.Format(fmt_amount_scale, numString, appendScale).Trim();
}

电流达到(短程)四度。只需更改thous变量即可添加其他支持(对于较大的数字或long scale)。

答案 3 :(得分:3)

  

这是西班牙语版本:

        public static string numeroALetras(int number)
    {
        if (number == 0)
            return "cero";

        if (number < 0)
            return "menos " + numeroALetras(Math.Abs(number));

        string words = "";

        if ((number / 1000000) > 0)
        {
            words += numeroALetras(number / 1000000) + " millón ";
            number %= 1000000;
        }

        if ((number / 1000) > 0)
        {
            words += (number / 1000) == 1? "mil ": numeroALetras(number / 1000) + " mil ";
            number %= 1000;
        }
        if ((number / 100) == 1)
        {
            if (number == 100)
                words += "cien";
            else words += (number / 100)> 1? numeroALetras(number / 100) + " ciento ":"ciento ";
            number %= 100;
        }
        if ((number / 100) > 1)
        {
            var hundredMap = new[] {"","", "dosc", "tresc", "cuatroc", "quin", "seisc", "sietec", "ochoc", "novec" };
            if (number > 199)
                words += hundredMap[number/100] + "ientos ";
            else {
                words += numeroALetras(number / 100) + " ientos ";
            }
            number %= 100;
        }

        if (number > 0)
        {
            if (words != "")
                words += " ";

            var unitsMap = new[] { "cero", "uno", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince", "dieciseis", "diecisiete", "dieciocho", "diecinueve", "veinte" };
            var tensMap = new[] { "cero", "diez", "veinti", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa" };

            if (number < 21)
                words += unitsMap[number];
            else
            {                    
                words += tensMap[number / 10];
                if ((number % 10) > 0)
                    words += ((number % 10)>2?" y ": "") + unitsMap[number % 10];                    
            }
        }

        return words;
    }

答案 4 :(得分:2)

Imports System.Text

Public Class NumberWriter

    Public Shared Function Parse(ByVal Number As String) As String
        If Not AreNumbers(Number) Then Return ""
        Dim TempQueue As New Queue(Of String)
        For Each ItemA As Char In Number.Replace(",", "").Reverse
            TempQueue.Enqueue(ItemA)
        Next
        Dim Blocks As New List(Of String)
        Dim BlockEmpty As New List(Of Boolean)
        Do
            Dim TempBlock As New StringBuilder(3)
            TempBlock.Append(TempQueue.Dequeue)
            If TempQueue.Count > 0 Then
                TempBlock.Append(TempQueue.Dequeue)
                If TempQueue.Count > 0 Then
                    TempBlock.Append(TempQueue.Dequeue)
                End If
            End If
            Blocks.Add(StrReverse(TempBlock.ToString))
            BlockEmpty.Add(TempBlock.ToString = "000")
            If TempQueue.Count < 1 Then Exit Do
        Loop
        Dim ResultStack As New Stack(Of String)
        For int1 As Integer = 0 To Blocks.Count - 1
            ResultStack.Push(ReadBlock(Blocks(int1)) & If(Not int1 = 0, If(Not BlockEmpty(int1), " " & CapitalizeWord(GetPlaceValueSet(int1)) & If(BlockEmpty(int1 - 1), "", ", "), ""), ""))
        Next
        Dim Result1 As String = ""
        Do Until ResultStack.Count < 1
            Result1 &= ResultStack.Pop
        Loop
        Return RemoveGrammarErrors(Result1)
    End Function

    Private Shared Function RemoveGrammarErrors(ByVal Str As String) As String
        Dim tstr As String = Str
        tstr.Replace("  ", " ")
        tstr.Replace(" , ", ", ")
        Return tstr
    End Function

    Private Shared Function AreNumbers(ByVal Str1 As String) As Boolean
        Dim Numbers() As String = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ","}
        For Each ItemA As Char In Str1
            Dim IsN As Boolean = False
            For Each ItemB As String In Numbers
                If ItemA = ItemB Then IsN = True
            Next
            If Not IsN Then
                Return False
            End If
        Next
        Return True
    End Function

    Private Shared Function ReadBlock(ByVal Block As String)
        Select Case Block.Length
            Case 1
                Return ReadSingleDigit(Block)
            Case 2
                Return ReadTwoDigits(Block)
            Case 3
                Return ReadThreeDigits(Block)
            Case Else
                Throw New Exception
        End Select
    End Function

    Private Shared Function ReadThreeDigits(ByVal Digits As String)
        If Digits.Length > 3 Then Throw New ArgumentException("There are too many digits.")
        Dim Result As String = ""
        If Not Digits(0) = "0" Then
            Result &= ReadSingleDigit(Digits(0)) & " Hundred "
        End If
        Result &= ReadTwoDigits(Digits.Substring(1))
        Return Result
    End Function

    Private Shared Function ReadTwoDigits(ByVal Digits As String)
        If Digits.Length > 2 Then Throw New ArgumentException("There are too many digits.")
        Select Case Digits(0)
            Case "0"
                Return ReadSingleDigit(Digits(1))
            Case "1"
                Return ReadTeenNumber(Digits)
            Case Else
                Return ReadFirstInNumberPair(Digits(0)) & If(Digits(1) = "0", "", "-" & ReadSingleDigit(Digits(1)))
        End Select
    End Function

    Private Shared Function ReadSingleDigit(ByVal Digit As String) As String
        If Not Digit.Length = 1 Then Throw New ArgumentException("There must be only one digit and it must be more than zero.")
        Select Case Digit
            Case "0"
                Return ""
            Case "1"
                Return "One"
            Case "2"
                Return "Two"
            Case "3"
                Return "Three"
            Case "4"
                Return "Four"
            Case "5"
                Return "Five"
            Case "6"
                Return "Six"
            Case "7"
                Return "Seven"
            Case "8"
                Return "Eight"
            Case "9"
                Return "Nine"
            Case Else
                Throw New Exception()
        End Select
    End Function

    Private Shared Function ReadTeenNumber(ByVal Num As String) As String
        Select Case Num
            Case "11"
                Return "Eleven"
            Case "12"
                Return "Twelve"
            Case "13"
                Return "Thirteen"
            Case "14"
                Return "Fourteen"
            Case "15"
                Return "Fifteen"
            Case "16"
                Return "Sixteen"
            Case "17"
                Return "Seventeen"
            Case "18"
                Return "Eighteen"
            Case "19"
                Return "Nineteen"
            Case Else
                Throw New Exception()
        End Select
    End Function

    Private Shared Function ReadFirstInNumberPair(ByVal Num As String) As String
        If Not (Num > 1 OrElse Num < 10) Then Throw New ArgumentException("Number must be more than 1 and less than 10")
        Select Case Num
            Case "2"
                Return "Twenty"
            Case "3"
                Return "Thirty"
            Case "4"
                Return "Fourty"
            Case "5"
                Return "Fifty"
            Case "6"
                Return "Sixty"
            Case "7"
                Return "Seventy"
            Case "8"
                Return "Eighty"
            Case "9"
                Return "Ninety"
            Case Else
                Throw New Exception()
        End Select
    End Function

    Private Shared Function CapitalizeWord(ByVal Word As String) As String
        Return Word.Substring(0, 1).ToUpper & Word.Substring(1)
    End Function

    Private Shared Function GetPlaceValueSet(ByVal Num As Byte) As String
        Select Case Num
            Case 0
                Return "" 'Hundreds
            Case 1
                Return "Thousand"
            Case 2
                Return "Million"
            Case 3
                Return "Billion"
            Case 4
                Return "Trillion"
            Case 5
                Return "Quadrillion"
            Case 6
                Return "Quintillion"
            Case 7
                Return "Sextillion"
            Case 8
                Return "Septillion"
            Case 9
                Return "Octillion"
            Case 10
                Return "Nonillion"
            Case 11
                Return "octillion"
            Case 12
                Return "nonillion"
            Case 13
                Return "decillion"
            Case 14
                Return "undecillion"
            Case 15
                Return "dodecillion,"
            Case 16
                Return "tredecillion"
            Case 17
                Return "quattuordecillion"
            Case 18
                Return "quindecillion"
            Case 19
                Return "sexdecillion"
            Case 20
                Return "septendecillion"
            Case 21
                Return "octodecillion"
            Case 22
                Return "novemdecillion"
            Case 23
                Return "vigintillion"
            Case 24
                Return "unvigintillion"
            Case 25
                Return "dovigintillion"
            Case 26
                Return "trevigintillion"
            Case 27
                Return "quattuorvigintillion"
            Case 28
                Return "quinvigintillion"
            Case 29
                Return "sexvigintillion"
            Case 30
                Return "septenvigintillion"
            Case 31
                Return "octovigintillion"
            Case 32
                Return "novemvigintillion"
            Case 33
                Return "trigintillion"
            Case 34
                Return "untrigintillion"
            Case 35
                Return "dotrigintillion"
            Case 36
                Return "tretrigintillion"
            Case 37
                Return "quattuortrigintillion"
            Case 38
                Return "quintrigintillion"
            Case 39
                Return "sextrigintillion"
            Case 40
                Return "septentrigintillion"
            Case 41
                Return "octotrigintillion"
            Case Else
                Throw New Exception
        End Select
    End Function
End Class

对不起,这是在VB.NET中,但它完全有效。这是一种方式。言语数量。处理数字长达123个字符我相信。

答案 5 :(得分:1)

http://www.exchangecore.com/blog/convert-number-words-c-sharp-console-application/有一些C#脚本,可以处理非常大的数字和非常小的小数。

using System;
using System.Collections.Generic;
using System.Text;

namespace NumWords
{
    class Program
    {
        // PROGRAM HANDLES NEGATIVE AND POSITIVE DOUBLES


        static String NumWordsWrapper(double n)
        {
            string words = "";
            double intPart;
            double decPart = 0;
            if (n == 0)
                return "zero";
            try {
                string[] splitter = n.ToString().Split('.');
                intPart = double.Parse(splitter[0]);
                decPart = double.Parse(splitter[1]);
            } catch {
                intPart = n;
            }

            words = NumWords(intPart);

            if (decPart > 0) {
                if (words != "")
                    words += " and ";
                int counter = decPart.ToString().Length;
                switch (counter) {
                    case 1: words += NumWords(decPart) + " tenths"; break;
                    case 2: words += NumWords(decPart) + " hundredths"; break;
                    case 3: words += NumWords(decPart) + " thousandths"; break;
                    case 4: words += NumWords(decPart) + " ten-thousandths"; break;
                    case 5: words += NumWords(decPart) + " hundred-thousandths"; break;
                    case 6: words += NumWords(decPart) + " millionths"; break;
                    case 7: words += NumWords(decPart) + " ten-millionths"; break;
                }
            }
            return words;
        }

        static String NumWords(double n) //converts double to words
        {
            string[] numbersArr = new string[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
            string[] tensArr = new string[] { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninty" };
            string[] suffixesArr = new string[] { "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "Quattuordecillion", "Quindecillion", "Sexdecillion", "Septdecillion", "Octodecillion", "Novemdecillion", "Vigintillion" };
            string words = "";

            bool tens = false;

            if (n < 0) {
                words += "negative ";
                n *= -1;
            }

            int power = (suffixesArr.Length + 1) * 3;

            while (power > 3) {
                double pow = Math.Pow(10, power);
                if (n >= pow) {
                    if (n % pow > 0) {
                        words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1] + ", ";
                    } else if (n % pow == 0) {
                        words += NumWords(Math.Floor(n / pow)) + " " + suffixesArr[(power / 3) - 1];
                    }
                    n %= pow;
                }
                power -= 3;
            }
            if (n >= 1000) {
                if (n % 1000 > 0) words += NumWords(Math.Floor(n / 1000)) + " thousand, ";
                else words += NumWords(Math.Floor(n / 1000)) + " thousand";
                n %= 1000;
            }
            if (0 <= n && n <= 999) {
                if ((int)n / 100 > 0) {
                    words += NumWords(Math.Floor(n / 100)) + " hundred";
                    n %= 100;
                }
                if ((int)n / 10 > 1) {
                    if (words != "")
                        words += " ";
                    words += tensArr[(int)n / 10 - 2];
                    tens = true;
                    n %= 10;
                }

                if (n < 20 && n > 0) {
                    if (words != "" && tens == false)
                        words += " ";
                    words += (tens ? "-" + numbersArr[(int)n - 1] : numbersArr[(int)n - 1]);
                    n -= Math.Floor(n);
                }
            }

            return words;

        }
        static void Main(string[] args)
        {
            Console.Write("Enter a number to convert to words: ");
            Double n = Double.Parse(Console.ReadLine());

            Console.WriteLine("{0}", NumWordsWrapper(n));
        }
    }
}

编辑:从博客文章中提取代码

答案 6 :(得分:1)

这是我的解决方案希望它能帮到你

namespace ConsoleApplication3
{

    class Program
    {


        static void Main(string[] args)
        {

            string s = Console.ReadLine();
            ConvertMyword(int.Parse(s));

            Console.Read();

        }

        static void ConvertMyword(int number)
        {
            int flag = 0;
            int lflag = 0;
            string words = String.Empty;
            string[] places = { "ones", "ten", "hundred", "thousand", "ten thousand", "lacs","tenlacs","crore","tencrore" };
            string rawnumber = number.ToString();
            char[] a = rawnumber.ToCharArray();
            Array.Reverse(a);
            for (int i = a.Length - 1; i >= 0; i--)
            {
                if (i % 2 == 0 && i > 2)
                {
                    if (int.Parse(a[i].ToString()) > 1)
                    {
                        if (int.Parse(a[i - 1].ToString()) == 0)
                        {
                            words = words + getNumberStringty(int.Parse(a[i].ToString())) + " " + places[i - 1] + " ";
                        }
                        else
                        {
                            words = words + getNumberStringty(int.Parse(a[i].ToString())) + " ";
                        }
                    }
                    else if (int.Parse(a[i].ToString()) == 1)
                    {
                        if (int.Parse(a[i - 1].ToString())== 0)
                        {
                            words = words +"Ten" + " ";
                        }
                        else
                        {
                            words = words + getNumberStringteen(int.Parse(a[i - 1].ToString())) + " ";
                        }
                        flag = 1;
                    }
                }
                else
                {
                    if (i == 1 || i == 0)
                    {
                        if (int.Parse(a[i].ToString()) > 1)
                        {
                            words = words + getNumberStringty(int.Parse(a[i].ToString())) + " " + getNumberString(int.Parse(a[0].ToString())) + " ";
                            break;
                        }
                        else if (int.Parse(a[i].ToString()) == 1)
                        {
                            if (int.Parse(a[i - 1].ToString()) == 0)
                            {
                                words = words + "Ten" + " ";
                            }
                            else
                            {
                                words = words + getNumberStringteen(int.Parse(a[i - 1].ToString())) + " ";
                            }

                            break;
                        }
                        else if (int.Parse(a[i - 1].ToString()) != 0)
                        {
                            words = words + getNumberString(int.Parse(a[i - 1].ToString())) + " ";
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (flag == 0)
                        {
                            for(int l=i;l>=0;l--)
                            {
                                if (int.Parse(a[l].ToString())!=0)
                                {
                                    lflag = 1;
                                }
                            }
                            if (lflag == 1 && int.Parse(a[i].ToString())!=0)
                            {

                                    words = words + getNumberString(int.Parse(a[i].ToString())) + " " + places[i] + " ";
                                    lflag = 0;


                            }
                            else if(lflag == 0)
                            {
                               // words = words + getNumberString(int.Parse(a[i].ToString())) + " " + places[i] + " ";
                                lflag = 0;
                                break;
                            }

                        }
                        else
                        {
                            words = words + " " + places[i] + " ";
                            flag = 0;
                        }

                    }
                }
            }
            Console.WriteLine(words);
        }
        static string getNumberString(int num)
        {
            string Word = String.Empty;
            switch (num)
            {
                case 1:
                    Word = "one";
                    break;
                case 2:
                    Word = "two";
                    break;

                case 3:
                    Word = "three";
                    break;

                case 4:
                    Word = "four";
                    break;

                case 5:
                    Word = "five";
                    break;

                case 6:
                    Word = "six";
                    break;
                case 7:
                    Word = "seven";
                    break;

                case 8:
                    Word = "eight";
                    break;

                case 9:
                    Word = "nine";
                    break;


            }
            return Word;
        }
        static string getNumberStringty(int num)
        {
            string Word = String.Empty;
            switch (num)
            {

                case 2:
                    Word = "twenty";
                    break;

                case 3:
                    Word = "thirty";
                    break;

                case 4:
                    Word = "fourty";
                    break;

                case 5:
                    Word = "fifty";
                    break;

                case 6:
                    Word = "sixty";
                    break;
                case 7:
                    Word = "seventy";
                    break;

                case 8:
                    Word = "eighty";
                    break;

                case 9:
                    Word = "ninty";
                    break;


            }
            return Word;
        }
        static string getNumberStringteen(int num)
        {
            string Word = String.Empty;
            switch (num)
            {
                    case 1:
                    Word = "eleven";
                    break;
                case 2:
                    Word = "tewlve";
                    break;

                case 3:
                    Word = "thirteen";
                    break;

                case 4:
                    Word = "fourteen";
                    break;

                case 5:
                    Word = "fifteen";
                    break;

                case 6:
                    Word = "sixteen";
                    break;
                case 7:
                    Word = "seventeen";
                    break;

                case 8:
                    Word = "eighteen";
                    break;

                case 9:
                    Word = "ninteen";
                    break;


            }
            return Word;
        }
    }

}

答案 7 :(得分:1)

如果有人想要JavaScript版本

Number.prototype.numberToWords = function () {
    var unitsMap = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
    var tensMap = ["zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];

    var num = this.valueOf();
    if (Math.round(num == 0)) {
        return "zero";
    }
    if (num < 0) {
        var positivenum = Math.abs(num);
        return "minus " + Number(positivenum).numberToWords();
    }
    var words = "";
    if (Math.floor(num / 1000000) > 0) {
        words += Math.floor(num / 1000000).numberToWords() + " million ";
        num = Math.floor(num % 1000000);
    }
    if (Math.floor(num / 1000) > 0) {
        words += Math.floor(num / 1000).numberToWords() + " thousand ";
        num = Math.floor(num % 1000);
    }
    if (Math.floor(num / 100) > 0) {
        words += Math.floor(num / 100).numberToWords() + " hundred ";
        num = Math.floor(num % 100);
    }
    if (Math.floor(num > 0)) {
        if (words != "") {
            words += "and ";
        }
        if (num < 20) {
        words += unitsMap[num];
        }
        else {
            words += tensMap[Math.floor(num / 10)];
            if ((num % 10) > 0) {
                words += "-" + unitsMap[Math.round(num % 10)];
            }
        }
    }
    return words.trim();
}

答案 8 :(得分:1)

我的任务是创建一个WEB API,使用C#将数字转换为单词。

可以是48小时内的整数或小数点。

调用将来自使用Ajax Post方法的前端应用程序,并将转换后的结果返回到网页中。

我已在GitHub上公开分享该项目以供参考:https://github.com/marvinglennlacuna/NumbersToWordsConverter.Api

通过以下技术实现:

  1. MVC结构化
  2. API控制器
  3. 服务
  4. 模型
  5. 错误处理
  6. 使用MSTest进行单元测试
  7. 代码覆盖率 - 98%
  8. Jquery的
  9. 关于以下内容的技术文档:

    1. 目的
    2. 先决条件
    3. 功能要求
    4. 流程图和输出
    5. **网页结果(US-001)**

        

      US-001通过网页流程将数字转换为单词

      enter image description here

        

      US-001通过网页输出将数字转换为单词

      enter image description here

      通过Postman的结果(US-002)

        

      US-002 - 通过邮递员流程将数字转换为单词

      enter image description here

        

      US-002 - 通过邮递员输出将数字转换为单词

      enter image description here

      我认为,如果您需要在面试/代码测试/学校或只是为了有趣的情况下参考,我们只需要分享一个有效的解决方案。

      干杯, 马文

答案 9 :(得分:0)

这个类完美地转换你的float或double(直到2精度)。 只需复制并粘贴到IDE中即可查看结果。

java.lang.Enum

答案 10 :(得分:0)

虽然这是一个老问题,但我已经用更详细的方法实现了这个功能

public static class NumberToWord
    {
        private static readonly Dictionary<long, string> MyDictionary = new Dictionary<long, string>();

        static NumberToWord()
        {
            MyDictionary.Add(1000000000000000, "quadrillion");
            MyDictionary.Add(1000000000000, "trillion");
            MyDictionary.Add(1000000000, "billion");
            MyDictionary.Add(1000000, "million");
            MyDictionary.Add(1000, "thousand");
            MyDictionary.Add(100, "hundread");
            MyDictionary.Add(90, "ninety");
            MyDictionary.Add(80, "eighty");
            MyDictionary.Add(70, "seventy");
            MyDictionary.Add(60, "sixty");
            MyDictionary.Add(50, "fifty");
            MyDictionary.Add(40, "fourty");
            MyDictionary.Add(30, "thirty");
            MyDictionary.Add(20, "twenty");
            MyDictionary.Add(19, "nineteen");
            MyDictionary.Add(18, "eighteen");
            MyDictionary.Add(17, "seventeen");
            MyDictionary.Add(16, "sixteen");
            MyDictionary.Add(15, "fifteen");
            MyDictionary.Add(14, "fourteen");
            MyDictionary.Add(13, "thirteen");
            MyDictionary.Add(12, "twelve");
            MyDictionary.Add(11, "eleven");
            MyDictionary.Add(10, "ten");
            MyDictionary.Add(9, "nine");
            MyDictionary.Add(8, "eight");
            MyDictionary.Add(7, "seven");
            MyDictionary.Add(6, "six");
            MyDictionary.Add(5, "five");
            MyDictionary.Add(4, "four");
            MyDictionary.Add(3, "three");
            MyDictionary.Add(2, "two");
            MyDictionary.Add(1, "one");
            MyDictionary.Add(0, "zero");
        }

        /// <summary>
        /// To the verbal.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static string ToVerbal(this int value)
        {
            return ToVerbal((long) value);
        }

        /// <summary>
        /// To the verbal.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static string ToVerbal(this long value)
        {
            if (value == 0) return MyDictionary[value];

            if (value < 0)
                return $" negative {ToVerbal(Math.Abs(value))}";

            var builder = new StringBuilder();

            for (var i = 1000000000000000; i >= 1000; i = i/1000)
                value = ConstructWord(value, builder, i);

            value = ConstructWord(value, builder, 100);

            for (var i = 90; i >= 20; i = i - 10)
                value = ConstructWordForTwoDigit(value, builder, i);

            if (MyDictionary.ContainsKey(value))
                builder.AppendFormat("{0}" + MyDictionary[value], builder.Length > 0 
                    ? " " 
                    : string.Empty);

            return builder.ToString();
        }

        private static long ConstructWord(long value, StringBuilder builder, long key)
        {
            if (value >= key)
            {
                var unit = (int) (value/key);
                value -= unit*key;
                builder.AppendFormat(" {0} {1} " + MyDictionary[key], builder.Length > 0
                    ? ", "
                    : string.Empty, ToVerbal(unit));
            }
            return value;
        }
        private static long ConstructWordForTwoDigit(long value, StringBuilder builder, long key)
        {
            if (value >= key)
            {
                value -= key;
                builder.AppendFormat(" {0} " + MyDictionary[key], builder.Length > 0
                    ? " "
                    : string.Empty);
            }
            return value;
        } 
    }

仅供参考:我有用户字符串插值,仅在4.6.1

中可用

答案 11 :(得分:0)

占用较少代码的解决方案。

最重要的部分只有几行:

<div style="width:100%; height:60px;">
<div style="float:right; height:100%;padding-right:1%"> 
<button style="display:inline-block; height:70%;background-color:green; color:white;border:none; padding:0;">
<span style="display:inline-block; max-width:10%; text-overflow:ellipses;overflow:hidden; max-height:100%;text-align:center;white-space:no-wrap;">John John</span>
</button>
</div>
</div>

此处提供完整代码https://dotnetfiddle.net/wjr4hF

答案 12 :(得分:0)

以下C#控制台应用程序代码将接受最多2位小数的货币值,并以英文打印。您可以将其用作参考来实现结果。

   namespace ConsoleApplication2
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    class Program
    {
       static void Main(string[] args)
        {
            bool repeat = true;
            while (repeat)
            {
                string inputMonetaryValueInNumberic = string.Empty;
                string centPart = string.Empty;
                string dollarPart = string.Empty;
                Console.Write("\nEnter the monetary value : ");
                inputMonetaryValueInNumberic = Console.ReadLine();
                inputMonetaryValueInNumberic = inputMonetaryValueInNumberic.TrimStart('0');

                if (ValidateInput(inputMonetaryValueInNumberic))
                {

                    if (inputMonetaryValueInNumberic.Contains('.'))
                    {
                        centPart = ProcessCents(inputMonetaryValueInNumberic.Substring(inputMonetaryValueInNumberic.IndexOf(".") + 1));
                        dollarPart = ProcessDollar(inputMonetaryValueInNumberic.Substring(0, inputMonetaryValueInNumberic.IndexOf(".")));
                    }
                    else
                    {
                        dollarPart = ProcessDollar(inputMonetaryValueInNumberic);
                    }
                    centPart = string.IsNullOrWhiteSpace(centPart) ? string.Empty : " and " + centPart;
                    Console.WriteLine(string.Format("\n\n{0}{1}", dollarPart, centPart));
                }
                else
                {
                    Console.WriteLine("Invalid Input..");
                }

                Console.WriteLine("\n\nPress any key to continue or Escape of close : ");
                var loop = Console.ReadKey();
                repeat = !loop.Key.ToString().Contains("Escape");
                Console.Clear();
            }

        }

        private static string ProcessCents(string cents)
        {
            string english = string.Empty;
            string dig3 = Process3Digit(cents);
            if (!string.IsNullOrWhiteSpace(dig3))
            {
                dig3 = string.Format("{0} {1}", dig3, GetSections(0));
            }
            english = dig3 + english;
            return english;
        }
        private static string ProcessDollar(string dollar)
        {
            string english = string.Empty;
            foreach (var item in Get3DigitList(dollar))
            {
                string dig3 = Process3Digit(item.Value);
                if (!string.IsNullOrWhiteSpace(dig3))
                {
                    dig3 = string.Format("{0} {1}", dig3, GetSections(item.Key));
                }
                english = dig3 + english;
            }
            return english;
        }
        private static string Process3Digit(string digit3)
        {
            string result = string.Empty;
            if (Convert.ToInt32(digit3) != 0)
            {
                int place = 0;
                Stack<string> monetaryValue = new Stack<string>();
                for (int i = digit3.Length - 1; i >= 0; i--)
                {
                    place += 1;
                    string stringValue = string.Empty;
                    switch (place)
                    {
                        case 1:
                            stringValue = GetOnes(digit3[i].ToString());
                            break;
                        case 2:
                            int tens = Convert.ToInt32(digit3[i]);
                            if (tens == 1)
                            {
                                if (monetaryValue.Count > 0)
                                {
                                    monetaryValue.Pop();
                                }
                                stringValue = GetTens((digit3[i].ToString() + digit3[i + 1].ToString()));
                            }
                            else
                            {
                                stringValue = GetTens(digit3[i].ToString());
                            }
                            break;
                        case 3:
                            stringValue = GetOnes(digit3[i].ToString());
                            if (!string.IsNullOrWhiteSpace(stringValue))
                            {
                                string postFixWith = " Hundred";
                                if (monetaryValue.Count > 0)
                                {
                                    postFixWith = postFixWith + " And";
                                }
                                stringValue += postFixWith;
                            }
                            break;
                    }
                    if (!string.IsNullOrWhiteSpace(stringValue))
                        monetaryValue.Push(stringValue);
                }
                while (monetaryValue.Count > 0)
                {
                    result += " " + monetaryValue.Pop().ToString().Trim();
                }
            }
            return result;
        }
        private static Dictionary<int, string> Get3DigitList(string monetaryValueInNumberic)
        {
            Dictionary<int, string> hundredsStack = new Dictionary<int, string>();
            int counter = 0;
            while (monetaryValueInNumberic.Length >= 3)
            {
                string digit3 = monetaryValueInNumberic.Substring(monetaryValueInNumberic.Length - 3, 3);
                monetaryValueInNumberic = monetaryValueInNumberic.Substring(0, monetaryValueInNumberic.Length - 3);
                hundredsStack.Add(++counter, digit3);
            }
            if (monetaryValueInNumberic.Length != 0)
                hundredsStack.Add(++counter, monetaryValueInNumberic);
            return hundredsStack;
        }
        private static string GetTens(string tensPlaceValue)
        {
            string englishEquvalent = string.Empty;
            int value = Convert.ToInt32(tensPlaceValue);
            Dictionary<int, string> tens = new Dictionary<int, string>();
            tens.Add(2, "Twenty");
            tens.Add(3, "Thirty");
            tens.Add(4, "Forty");
            tens.Add(5, "Fifty");
            tens.Add(6, "Sixty");
            tens.Add(7, "Seventy");
            tens.Add(8, "Eighty");
            tens.Add(9, "Ninty");
            tens.Add(10, "Ten");
            tens.Add(11, "Eleven");
            tens.Add(12, "Twelve");
            tens.Add(13, "Thrteen");
            tens.Add(14, "Fourteen");
            tens.Add(15, "Fifteen");
            tens.Add(16, "Sixteen");
            tens.Add(17, "Seventeen");
            tens.Add(18, "Eighteen");
            tens.Add(19, "Ninteen");
            if (tens.ContainsKey(value))
            {
                englishEquvalent = tens[value];
            }

            return englishEquvalent;

        }
        private static string GetOnes(string onesPlaceValue)
        {
            int value = Convert.ToInt32(onesPlaceValue);
            string englishEquvalent = string.Empty;
            Dictionary<int, string> ones = new Dictionary<int, string>();
            ones.Add(1, " One");
            ones.Add(2, " Two");
            ones.Add(3, " Three");
            ones.Add(4, " Four");
            ones.Add(5, " Five");
            ones.Add(6, " Six");
            ones.Add(7, " Seven");
            ones.Add(8, " Eight");
            ones.Add(9, " Nine");

            if (ones.ContainsKey(value))
            {
                englishEquvalent = ones[value];
            }

            return englishEquvalent;
        }
        private static string GetSections(int section)
        {
            string sectionName = string.Empty;
            switch (section)
            {
                case 0:
                    sectionName = "Cents";
                    break;
                case 1:
                    sectionName = "Dollars";
                    break;
                case 2:
                    sectionName = "Thousand";
                    break;
                case 3:
                    sectionName = "Million";
                    break;
                case 4:
                    sectionName = "Billion";
                    break;
                case 5:
                    sectionName = "Trillion";
                    break;
                case 6:
                    sectionName = "Zillion";
                    break;
            }
            return sectionName;
        }
        private static bool ValidateInput(string input)
        {
            return Regex.IsMatch(input, "[0-9]{1,18}(\\.[0-9]{1,2})?"))
        }
    }
}

namespace ConsoleApplication2 { using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { bool repeat = true; while (repeat) { string inputMonetaryValueInNumberic = string.Empty; string centPart = string.Empty; string dollarPart = string.Empty; Console.Write("\nEnter the monetary value : "); inputMonetaryValueInNumberic = Console.ReadLine(); inputMonetaryValueInNumberic = inputMonetaryValueInNumberic.TrimStart('0'); if (ValidateInput(inputMonetaryValueInNumberic)) { if (inputMonetaryValueInNumberic.Contains('.')) { centPart = ProcessCents(inputMonetaryValueInNumberic.Substring(inputMonetaryValueInNumberic.IndexOf(".") + 1)); dollarPart = ProcessDollar(inputMonetaryValueInNumberic.Substring(0, inputMonetaryValueInNumberic.IndexOf("."))); } else { dollarPart = ProcessDollar(inputMonetaryValueInNumberic); } centPart = string.IsNullOrWhiteSpace(centPart) ? string.Empty : " and " + centPart; Console.WriteLine(string.Format("\n\n{0}{1}", dollarPart, centPart)); } else { Console.WriteLine("Invalid Input.."); } Console.WriteLine("\n\nPress any key to continue or Escape of close : "); var loop = Console.ReadKey(); repeat = !loop.Key.ToString().Contains("Escape"); Console.Clear(); } } private static string ProcessCents(string cents) { string english = string.Empty; string dig3 = Process3Digit(cents); if (!string.IsNullOrWhiteSpace(dig3)) { dig3 = string.Format("{0} {1}", dig3, GetSections(0)); } english = dig3 + english; return english; } private static string ProcessDollar(string dollar) { string english = string.Empty; foreach (var item in Get3DigitList(dollar)) { string dig3 = Process3Digit(item.Value); if (!string.IsNullOrWhiteSpace(dig3)) { dig3 = string.Format("{0} {1}", dig3, GetSections(item.Key)); } english = dig3 + english; } return english; } private static string Process3Digit(string digit3) { string result = string.Empty; if (Convert.ToInt32(digit3) != 0) { int place = 0; Stack<string> monetaryValue = new Stack<string>(); for (int i = digit3.Length - 1; i >= 0; i--) { place += 1; string stringValue = string.Empty; switch (place) { case 1: stringValue = GetOnes(digit3[i].ToString()); break; case 2: int tens = Convert.ToInt32(digit3[i]); if (tens == 1) { if (monetaryValue.Count > 0) { monetaryValue.Pop(); } stringValue = GetTens((digit3[i].ToString() + digit3[i + 1].ToString())); } else { stringValue = GetTens(digit3[i].ToString()); } break; case 3: stringValue = GetOnes(digit3[i].ToString()); if (!string.IsNullOrWhiteSpace(stringValue)) { string postFixWith = " Hundred"; if (monetaryValue.Count > 0) { postFixWith = postFixWith + " And"; } stringValue += postFixWith; } break; } if (!string.IsNullOrWhiteSpace(stringValue)) monetaryValue.Push(stringValue); } while (monetaryValue.Count > 0) { result += " " + monetaryValue.Pop().ToString().Trim(); } } return result; } private static Dictionary<int, string> Get3DigitList(string monetaryValueInNumberic) { Dictionary<int, string> hundredsStack = new Dictionary<int, string>(); int counter = 0; while (monetaryValueInNumberic.Length >= 3) { string digit3 = monetaryValueInNumberic.Substring(monetaryValueInNumberic.Length - 3, 3); monetaryValueInNumberic = monetaryValueInNumberic.Substring(0, monetaryValueInNumberic.Length - 3); hundredsStack.Add(++counter, digit3); } if (monetaryValueInNumberic.Length != 0) hundredsStack.Add(++counter, monetaryValueInNumberic); return hundredsStack; } private static string GetTens(string tensPlaceValue) { string englishEquvalent = string.Empty; int value = Convert.ToInt32(tensPlaceValue); Dictionary<int, string> tens = new Dictionary<int, string>(); tens.Add(2, "Twenty"); tens.Add(3, "Thirty"); tens.Add(4, "Forty"); tens.Add(5, "Fifty"); tens.Add(6, "Sixty"); tens.Add(7, "Seventy"); tens.Add(8, "Eighty"); tens.Add(9, "Ninty"); tens.Add(10, "Ten"); tens.Add(11, "Eleven"); tens.Add(12, "Twelve"); tens.Add(13, "Thrteen"); tens.Add(14, "Fourteen"); tens.Add(15, "Fifteen"); tens.Add(16, "Sixteen"); tens.Add(17, "Seventeen"); tens.Add(18, "Eighteen"); tens.Add(19, "Ninteen"); if (tens.ContainsKey(value)) { englishEquvalent = tens[value]; } return englishEquvalent; } private static string GetOnes(string onesPlaceValue) { int value = Convert.ToInt32(onesPlaceValue); string englishEquvalent = string.Empty; Dictionary<int, string> ones = new Dictionary<int, string>(); ones.Add(1, " One"); ones.Add(2, " Two"); ones.Add(3, " Three"); ones.Add(4, " Four"); ones.Add(5, " Five"); ones.Add(6, " Six"); ones.Add(7, " Seven"); ones.Add(8, " Eight"); ones.Add(9, " Nine"); if (ones.ContainsKey(value)) { englishEquvalent = ones[value]; } return englishEquvalent; } private static string GetSections(int section) { string sectionName = string.Empty; switch (section) { case 0: sectionName = "Cents"; break; case 1: sectionName = "Dollars"; break; case 2: sectionName = "Thousand"; break; case 3: sectionName = "Million"; break; case 4: sectionName = "Billion"; break; case 5: sectionName = "Trillion"; break; case 6: sectionName = "Zillion"; break; } return sectionName; } private static bool ValidateInput(string input) { return Regex.IsMatch(input, "[0-9]{1,18}(\\.[0-9]{1,2})?")) } } }

答案 13 :(得分:-1)

我实际上需要这个用于我正在开发的应用程序,但对此处的任何解决方案都不满意。仅供参考,此解决方案利用C#7.0对本地功能的支持。我还使用了新的数字分隔符来使更大的数字更具可读性。

public static class NumberExtensions
{
    private const string negativeWord = "negative";
    private static readonly Dictionary<ulong, string> _wordMap = new Dictionary<ulong, string>
    {
        [1_000_000_000_000_000_000] = "quintillion",
        [1_000_000_000_000_000] = "quadrillion",
        [1_000_000_000_000] = "trillion",
        [1_000_000_000] = "billion",
        [1_000_000] = "million",
        [1_000] = "thousand",
        [100] = "hundred",
        [90] = "ninety",
        [80] = "eighty",
        [70] = "seventy",
        [60] = "sixty",
        [50] = "fifty",
        [40] = "forty",
        [30] = "thirty",
        [20] = "twenty",
        [19] = "nineteen",
        [18] = "eighteen",
        [17] = "seventeen",
        [16] = "sixteen",
        [15] = "fifteen",
        [14] = "fourteen",
        [13] = "thirteen",
        [12] = "twelve",
        [11] = "eleven",
        [10] = "ten",
        [9] = "nine",
        [8] = "eight",
        [7] = "seven",
        [6] = "six",
        [5] = "five",
        [4] = "four",
        [3] = "three",
        [2] = "two",
        [1] = "one",
        [0] = "zero"
    };

    public static string ToWords(this short num)
    {
        var words = ToWords((ulong)Math.Abs(num));
        return num < 0 ? $"{negativeWord} {words}" : words;
    }

    public static string ToWords(this ushort num)
    {
        return ToWords((ulong)num);
    }

    public static string ToWords(this int num)
    {
        var words = ToWords((ulong)Math.Abs(num));
        return num < 0 ? $"{negativeWord} {words}" : words;
    }

    public static string ToWords(this uint num)
    {
        return ToWords((ulong)num);
    }

    public static string ToWords(this long num)
    {
        var words = ToWords((ulong)Math.Abs(num));
        return num < 0 ? $"{negativeWord} {words}" : words;
    }

    public static string ToWords(this ulong num)
    {
        var sb = new StringBuilder();
        var delimiter = String.Empty;

        void AppendWords(ulong dividend)
        {
            void AppendDelimitedWord(ulong key)
            {
                sb.Append(delimiter);
                sb.Append(_wordMap[key]);
                delimiter = 20 <= key && key < 100 ? "-" : " ";
            }

            if (_wordMap.ContainsKey(dividend))
            {
                AppendDelimitedWord(dividend);
            }
            else
            {
                var divisor = _wordMap.First(m => m.Key <= dividend).Key;
                var quotient = dividend / divisor;
                var remainder = dividend % divisor;

                if (quotient > 0 && divisor >= 100)
                {
                    AppendWords(quotient);
                }

                AppendDelimitedWord(divisor);

                if (remainder > 0)
                {   
                    AppendWords(remainder);
                }
            }
        }

        AppendWords(num);
        return sb.ToString();
    }    
}

肉在最后ToWords超载。