Как преобразовать число в слова в visual studio 2008 - PullRequest
0 голосов
/ 17 сентября 2010

Может ли кто-нибудь научить меня, как переводить числа в слова? Например:

18,000.00

На словах:

eighteen thousand

Надеюсь, кто-нибудь может помочь. Я использую профессиональную версию Visual Studio 2008.

Ответы [ 4 ]

0 голосов
/ 01 июля 2014

Я бы предпочел:

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 голосов
/ 17 сентября 2010

Вот логика, разработанная Джастином Роджерсом :

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;
    }
}
0 голосов
/ 08 марта 2011

Вот ответ для всех числовых значений вплоть до указанного предела для операций с плавающей запятой в VB и VBA.

Все числа, превышающие один «триллион», рекурсивны: квадриллионы и квинтиллионы выражаются в форме «Одна тысяча девятьсот шесть триллионов», и все остальные триллионы, миллионы, тысячи, сотни, десятки и цифры остаются учитывается в мелкой наличности.

Таким образом, вы можете выразить 2 50 как:

Одиннадцатьсот двадцать пять триллионов, восемьсот девяносто девять миллиардов, девятьсот шесть миллионов, восемьсот сорок две тысячи шестьсот двадцать Один триллион, девяносто девять миллиардов, пятьсот одиннадцать миллионов, шестьсот двадцать семь тысяч, семьсот семьдесят шесть

Я не ожидаю, что вам нужно будет регулярно указывать такие цифры в тексте. Однако, есть некоторая обучающая ценность в демонстрации применения рекурсии для начинающих в программировании.

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

0 голосов
/ 17 сентября 2010

Алгоритм высокого уровня:

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

Шаг перевода трехзначного кода должен был бы проверить число сотен и получить его строковый эквивалент + "сто и", если он больше нуля.использоваться для индексации в строковом массиве, если <20, или обрабатываться отдельно для индексирования строковых массивов для каждой цифры. </p>

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...