开发者

How to convert number to words in visual studio 2008

Can anyone teach me how开发者_如何学运维 to convert numbers to words? For example:

18,000.00

In words:

eighteen thousand

Hope anyone can help. I am using Visual Studio 2008 professional edition.


High level algorithm:

Break the number into three-digit groups
For each group {
    if value > 0 {
        Compute string translation
        output string + group identifier (eg. million, thousand, etc)
    }
}

The three-digit translation step would need to examine the hundreds digit and out it's string equivalent + " hundred and " if it's greater than 0. The remaining two digits could be used to index into a string array if < 20, or handled separately to index string arrays for each digit.


Here is the logic developed by Justin Rogers:

using System;

public class NumberToEnglish {
    private static string[] onesMapping = 
        new string[] {
            "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
            "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
            "Sixteen", "Seventeen", "Eighteen", "Nineteen"
        };
    private static string[] tensMapping =
        new string[] {
            "Twenty", "Thirty", "Forty", "Fifty", "Sixty", 
            "Seventy", "Eighty", "Ninety"
        };
    private static string[] groupMapping =
        new string[] {
            "Hundred", "Thousand", "Million", "Billion", "Trillion"
        };

    private static void Main(string[] args) {
        Console.WriteLine(EnglishFromNumber(long.Parse(args[0])));
    }

    private static string EnglishFromNumber(int number) {
        return EnglishFromNumber((long) number);
    }

    private static string EnglishFromNumber(long number) {
        if ( number == 0 ) {
            return onesMapping[number];
        }

        string sign = "Positive";
        if ( number < 0 ) {
            sign = "Negative";
            number = Math.Abs(number);
        }

        string retVal = null;
        int group = 0;
        while(number > 0) {
            int numberToProcess = (int) (number % 1000);
            number = number / 1000;

            string groupDescription = ProcessGroup(numberToProcess);
            if ( groupDescription != null ) {
                if ( group > 0 ) {
                    retVal = groupMapping[group] + " " + retVal;
                }
                retVal = groupDescription + " " + retVal;
            }

            group++;
        }

        return sign + " " + retVal;
    }

    private static string ProcessGroup(int number) {
        int tens = number % 100;
        int hundreds = number / 100;

        string retVal = null;
        if ( hundreds > 0 ) {
            retVal = onesMapping[hundreds] + " " + groupMapping[0];
        }
        if ( tens > 0 ) {
            if ( tens < 20 ) {
                retVal += ((retVal != null) ? " " : "") + onesMapping[tens];
            } else {
                int ones = tens % 10;
                tens = (tens / 10) - 2; // 20's offset

                retVal += ((retVal != null) ? " " : "") + tensMapping[tens];

                if ( ones > 0 ) {
                    retVal += ((retVal != null) ? " " : "") + onesMapping[ones];
                }
            }
        }

        return retVal;
    }
}


Here's the answer for all numeric values up to the specified limit for floating-point operations in VB and VBA.

All numbers exceeding one 'trillion' are recursed: quadrillions and quintillions are expressed in the form 'One thousand, nine hundred and six trillion' and whatever trivial quantity of billions, millions, thousands, hundreds, tens and digits remains to be accounted in petty cash.

You can therefore express 250 as:

Eleven Hundred and Twenty-Five Trillion, Eight Hundred and Ninety-Nine Billion, Nine Hundred and Six Million, Eight Hundred and Forty-Two Thousand, Six Hundred and Twenty One Trillion, Ninety-Nine Billion, Five Hundred and Eleven Million, Six Hundred and Twenty-Seven Thousand, Seven Hundred and Seventy-Six

I do not anticipate that you will need to express this kind of number in text on a regular basis. However, there is some instructional value in demonstrating an application of recursion to beginners in programming.

Public Function SayNumber(ByVal InputNumber As Double, Optional DecimalPlaces As Integer = 0) As String

' Return the integer portion of the number in formatted English words

' Return the fractional part of the number as 'point' and a series of ' single-numeral words, up to the precision specified by 'DecimalPlaces'

'   SayNumber(17241021505) '   "Seventeen Billion, Two Hundred and Forty-One Million, Twenty-One Thousand, Five Hundred and Five"

'   SayNumber(Sqr(2), 6) '   "One point Four One Four Two One Four"

' Note that nothing after the decimal point will be returned if InputNumber is an integer

' Nigel Heffernan, December 2008

Application.Volatile False On Error Resume Next

Dim arrDigits(0 To 9)   As String Dim arrTeens(10 To 19)  As String Dim arrTens(2 To 9)     As String

Dim i            As Integer Dim i10          As Integer Dim i20          As Integer Dim dblRemainder As Double Dim dblMain      As Double Dim iRemainder   As Long Dim iMain        As Long Dim dblFraction  As Double Dim strFraction  As String

Dim strMinus     As String Dim str1         As String Dim str2         As String Dim str3         As String

If Application.EnableEvents = False Then     Exit Function End If

arrDigits(0) = "Zero" arrDigits(1) = "One" arrDigits(2) = "Two" arrDigits(3) = "Three" arrDigits(4) = "Four" arrDigits(5) = "Five" arrDigits(6) = "Six" arrDigits(7) = "Seven" arrDigits(8) = "Eight" arrDigits(9) = "Nine"              arrTeens(10) = "Ten" arrTeens(11) = "Eleven" arrTeens(12) = "Twelve" arrTeens(13) = "Thirteen" arrTeens(14) = "Fourteen" arrTeens(15) = "Fifteen" arrTeens(16) = "Sixteen" arrTeens(17) = "Seventeen" arrTeens(18) = "Eighteen" arrTeens(19) = "Nineteen"

arrTens(2) = "Twenty" arrTens(3) = "Thirty" arrTens(4) = "Forty" arrTens(5) = "Fifty" arrTens(6) = "Sixty" arrTens(7) = "Seventy" arrTens(8) = "Eighty" arrTens(9) = "Ninety"

If InputNumber < 0 Then     strMinus = "Minus "     InputNumber = Abs(InputNumber) End If

If DecimalPlaces < 1 Then     strFraction = "" Else

    dblFraction = InputNumber - Fix(InputNumber)     If dblFraction = 0 Then         strFraction = ""     Else         strFraction = " point"         str1 = Format(dblFraction, "0." & String(DecimalPlaces, "0"))         For i = 1 To DecimalPlaces             str2 = MID(str1, i + 2, 1)             strFraction = strFraction & " " & arrDigits(CInt(str2))             str2 = ""         Next i         str1 = ""     End If      End If

If InputNumber < 10 Then

    str1 = arrDigits(InputNumber)      ElseIf InputNumber < 19 Then

    str1 = arrTeens(InputNumber)      ElseIf InputNumber < 100 Then          iMain = InputNumber \ 10     str1 = arrTens(iMain)          iRemainder = InputNumber Mod 10     If iRemainder > 0 Then         str2 = "-" & arrDigits(iRemainder)     End If

ElseIf InputNumber < 1000 Then

    iMain = InputNumber \ 100     str1 = arrDigits(iMain) & " Hundred"          iRemainder = InputNumber Mod 100     If iRemainder > 0 Then         str2 = " and " & SayNumber(iRemainder)     End If      ElseIf InputNumber < 2000 Then

    iMain = InputNumber \ 100     str1 = arrTeens(iMain) & " Hundred"          iRemainder = InputNumber Mod 100     If iRemainder > 0 Then         str2 = " and " & SayNumber(iRemainder)     End If      ElseIf InputNumber < 1000000 Then

    iMain = InputNumber \ 1000     str1 = SayNumber(iMain) & " Thousand"

    iRemainder = InputNumber Mod 1000     If iRemainder > 0 Then         str2 = ", " & SayNumber(iRemainder)     End If      ElseIf InputNumber < (10 ^ 9) Then

    iMain = InputNumber \ (10 ^ 6)     str1 = SayNumber(iMain) & " Million"

    iRemainder = InputNumber Mod (10 ^ 6)     If iRemainder > 0 Then         str2 = ", " & SayNumber(iRemainder)     End If      ElseIf InputNumber < (10 ^ 12) Then  ' we'll hit the LongInt arithmetic operation limit at ~2.14 Billion

    str3 = Format(InputNumber, "0")     dblMain = CDbl(Left(str3, Len(str3) - 9))     str1 = SayNumber(dblMain) & " Billion"          dblRemainder = CDbl(Right(str3, 9))     If dblRemainder > 0 Then         str2 = ", " & SayNumber(dblRemainder)     End If          ElseIf InputNumber < 1.79769313486231E+308 Then       ' This will generate a recursive string of 'Trillions'

    str3 = Format(InputNumber, "0")     dblMain = CDbl(Left(str3, Len(str3) - 12))     str1 = SayNumber(dblMain) & " Trillion"          dblRemainder = CDbl(Right(str3, 12))     If dblRemainder > 0 Then         str2 = ", " & SayNumber(dblRemainder)     End If          Else ' exceeds the specification for double-precision floating-point variables

    str1 = "#Overflow."          End If

SayNumber = strMinus & str1 & str2 & strFraction

End Function


I'd prefer:

Sub M_snb()
    MsgBox F_snb(InputBox(String(4, vbLf) & "enter a number", "snb")), , "snb"
End Sub

Function F_snb(y)
    F_snb = "invalid input"
    If y = 0 Then F_snb = "zero"
    If y = "" Or Val(y) = 0 Then Exit Function

    c00 = Format(Val(1 * y), String(3 * ((Len(Format(Val(1 * y))) - 1) \ 3 + 1), "0"))

    For j = 1 To Len(c00) \ 3
        x = Mid(c00, 3 * (j - 1) + 1, 3)

        sp = Array(F_mats(Left(x, 1)), F_mats(Val(Right(x, 2))), F_mats(Right(x, 1)), F_mats(Mid(x, 2, 1) & "0"), F_mats(Mid(x, 2, 1)))
        c01 = c01 & IIf(sp(0) = "", "", sp(0) & " Hundred ") & IIf(Right(x, 2) = "00", "", IIf(sp(1) <> "", sp(1), IIf(Mid(x, 2, 1) = "1", Trim(sp(2)) & "teen", IIf(sp(3) = "", sp(4) & "ty", sp(3)) & " " & sp(2)))) & Choose(Len(c00) \ 3 - j + 1, "", " Thousand ", " Million ", " Billion ")
    Next

    F_snb = Trim(Replace(c01, "  ", " "))
End Function

Function F_mats(y)
    On Error Resume Next

    F_mats = Split(Split(" 0 1One 2Two 3Three 4Four 5Five 6Six 7Seven 8Eight 9Nine 10Ten 11Eleven 12Twelve 13Thirteen 15Fifteen 20Twenty 30Thirty 40Forty 50Fifty 80Eighty ", y)(1))(0)
End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜