C#Windows应用程序中的数字到文本转换 [英] Number To Text Conversion in C# Windows Application

查看:104
本文介绍了C#Windows应用程序中的数字到文本转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请使用VB中编写的C#转换以下代码.

在这里-

Please Convert The following Code in C# Which is Written in VB.

Here--

Public Class NumeriCon

    Public Shared Function ConvertNum(ByVal Input As Long) As String ''Call this function passing the number you desire to be changed
        Dim output As String = Nothing
        If Input < 1000 Then
            output = FindNumber(Input) ''if its less than 1000 then just look it up
        Else
            Dim nparts() As String ''used to break the number up into 3 digit parts
            Dim n As String = Input ''string version of the number
            Dim i As Long = Input.ToString.Length ''length of the string to help break it up

            Do Until i - 3 <= 0
                n = n.Insert(i - 3, ",") ''insert commas to use as splitters
                i = i - 3 ''this insures that we get the correct number of parts
            Loop
            nparts = n.Split(",") ''split the string into the array

            i = Input.ToString.Length ''return i to initial value for reuse
            Dim p As Integer = 0 ''p for parts, used for finding correct suffix
            For Each s As String In nparts
                Dim x As Long = CLng(s) ''x is used to compare the part value to other values
                p = p + 1
                If p = nparts.Length Then ''if p = number of elements in the array then we need to do something different
                    If x <> 0 Then
                        If CLng(s) < 100 Then
                            output = output & " And " & FindNumber(CLng(s)) '' look up the number, no suffix 
                        Else                                                '' required as this is the last part
                            output = output & " " & FindNumber(CLng(s))
                        End If
                    End If
                Else ''if its not the last element in the array
                    If x <> 0 Then
                        If output = Nothing Then ''we have to check this so we don''t add a leading space
                            output = output & FindNumber(CLng(s)) & " " & FindSuffix(i, CLng(s)) ''look up the number and suffix
                        Else ''spaces must go in the right place
                            output = output & " " & FindNumber(CLng(s)) & " " & FindSuffix(i, CLng(s)) ''look up the snumber and suffix
                        End If
                    End If
                End If
                i = i - 3 ''reduce the suffix counter by 3 to step down to the next suffix
            Next
        End If
        Return output
    End Function

    Private Shared Function FindNumber(ByVal Number As Long) As String
        Dim Words As String = Nothing
        Dim Digits() As String = {"Zero","One","Two","Three","Four","Five","Six","Seven", _
      "Eight","Nine","Ten"}
        Dim Teens() As String = {"", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", _
       "Eighteen", "Nineteen"}

        If Number < 11 Then
            Words = Digits(Number)

        ElseIf Number < 20 Then
            Words = Teens(Number - 10)

        ElseIf Number = 20 Then
            Words = "Twenty"

        ElseIf Number < 30 Then
            Words = "Twenty " & Digits(Number - 20)

        ElseIf Number = 30 Then
            Words = "Thirty"

        ElseIf Number < 40 Then
            Words = "Thirty " & Digits(Number - 30)

        ElseIf Number = 40 Then
            Words = "Fourty"

        ElseIf Number < 50 Then
            Words = "Fourty " & Digits(Number - 40)

        ElseIf Number = 50 Then
            Words = "Fifty"

        ElseIf Number < 60 Then
            Words = "Fifty " & Digits(Number - 50)

        ElseIf Number = 60 Then
            Words = "Sixty"

        ElseIf Number < 70 Then
            Words = "Sixty " & Digits(Number - 60)

        ElseIf Number = 70 Then
            Words = "Seventy"

        ElseIf Number < 80 Then
            Words = "Seventy " & Digits(Number - 70)

        ElseIf Number = 80 Then
            Words = "Eighty"

        ElseIf Number < 90 Then
            Words = "Eighty " & Digits(Number - 80)

        ElseIf Number = 90 Then
            Words = "Ninety"

        ElseIf Number < 100 Then
            Words = "Ninety " & Digits(Number - 90)

        ElseIf Number < 1000 Then
            Words = Number.ToString
            Words = Words.Insert(1, ",")
            Dim wparts As String() = Words.Split(",")
            Words = FindNumber(wparts(0)) & " " & "Hundred"
            Dim n As String = FindNumber(wparts(1))
            If CLng(wparts(1)) <> 0 Then
                Words = Words & " And " & n
            End If
        End If

        Return Words
    End Function

    Private Shared Function FindSuffix(ByVal Length As Long, ByVal l As Long) As String
        Dim word As String

        If l <> 0 Then
            If Length > 12 Then
                word = "Trillion"
            ElseIf Length > 9 Then
                word = "Billion"
            ElseIf Length > 6 Then
                word = "Million"
            ElseIf Length > 3 Then
                word = "Thousand"
            ElseIf Length > 2 Then
                word = "Hundred"
            Else
                word = ""
            End If
        Else
            word = ""
        End If

        Return word
    End Function

End Class

推荐答案

有很多工具可以将vb转换为C#.
请检查一下.

http://www.developerfusion.com/tools/convert/vb-to-csharp/ [ ^ ]
There are so many tools to convert vb to C#.
Please check this.

http://www.developerfusion.com/tools/convert/vb-to-csharp/[^]


哇,您真的认为这里的人有足够的自由来从事您的工作吗?有些人愿意为您这样做,但是通常可以在 www.rentacoder.com [ http://www.developerfusion.com/tools/convert/vb-to-csharp/
Wow, do you really think that people here are free enough to do you work? There are people who would be willing to do this for you, but they are usually found at www.rentacoder.com[^]

Anyways, if you would have googled it; there are many tools online which do it for you (FREE). This is one of them, (and quite nice)

http://www.developerfusion.com/tools/convert/vb-to-csharp/


public class NumeriCon {

    public static string ConvertNum(long Input) {
        // Call this function passing the number you desire to be changed
        string output = null;
        if ((Input + lt)) {
            1000;
            output = FindNumber(Input);
            // if its less than 1000 then just look it up
        }
        else {
            string[] nparts;
            // used to break the number up into 3 digit parts
            string n = Input;
            // string version of the number
            long i = Input.ToString.Length;
            // length of the string to help break it up
            for (
            ; ((i - 3)
                        + lt);
            ) {
                0;
                n = n.Insert((i - 3), ",");
                // insert commas to use as splitters
                i = (i - 3);
                // this insures that we get the correct number of parts
            }
            nparts = n.Split(",");
            // split the string into the array
            i = Input.ToString.Length;
            // return i to initial value for reuse
            int p = 0;
            // p for parts, used for finding correct suffix
            foreach (string s in nparts) {
                long x = long.Parse(s);
                p = (p + 1);
                if ((p == nparts.Length)) {
                    // if p = number of elements in the array then we need to do something different
                    if ((x + lt)) {
                        gt;
                        0;
                        if ((long.Parse(s) + lt)) {
                            100;
                            output = (output + amp);
                            (" And " + amp);
                            FindNumber(long.Parse(s));
                            //  look up the number, no suffix
                        }
                        else {
                            //  required as this is the last part
                            output = (output + amp);
                            (" " + amp);
                            FindNumber(long.Parse(s));
                        }
                    }
                }
                else {
                    // if its not the last element in the array
                    if ((x + lt)) {
                        gt;
                        0;
                        if ((output == null)) {
                            // we have to check this so we don''t add a leading space
                            output = (output + amp);
                            (FindNumber(long.Parse(s)) + amp);
                            (" " + amp);
                            FindSuffix(i, long.Parse(s));
                            // look up the number and suffix
                        }
                        else {
                            // spaces must go in the right place
                            output = (output + amp);
                            (" " + amp);
                            (FindNumber(long.Parse(s)) + amp);
                            (" " + amp);
                            FindSuffix(i, long.Parse(s));
                            // look up the snumber and suffix
                        }
                    }
                }
                i = (i - 3);
                // reduce the suffix counter by 3 to step down to the next suffix
            }
        }
        return output;
    }

    private static string FindNumber(long Number) {
        string Words = null;
        string[] Digits;
        "Zero";
        "One";
        "Two";
        "Three";
        "Four";
        "Five";
        "Six";
        "Seven";
        "Eight";
        "Nine";
        "Ten";
        string[] Teens;
        "";
        "Eleven";
        "Twelve";
        "Thirteen";
        "Fourteen";
        "Fifteen";
        "Sixteen";
        "Seventeen";
        "Eighteen";
        "Nineteen";
        if ((Number + lt)) {
            11;
            Words = Digits[Number];
        }
        else if ((Number + lt)) {
            20;
            Words = Teens[(Number - 10)];
        }
        else if ((Number == 20)) {
            Words = "Twenty";
        }
        else if ((Number + lt)) {
            30;
            Words = ("Twenty " + amp);
            Digits[(Number - 20)];
        }
        else if ((Number == 30)) {
            Words = "Thirty";
        }
        else if ((Number + lt)) {
            40;
            Words = ("Thirty " + amp);
            Digits[(Number - 30)];
        }
        else if ((Number == 40)) {
            Words = "Fourty";
        }
        else if ((Number + lt)) {
            50;
            Words = ("Fourty " + amp);
            Digits[(Number - 40)];
        }
        else if ((Number == 50)) {
            Words = "Fifty";
        }
        else if ((Number + lt)) {
            60;
            Words = ("Fifty " + amp);
            Digits[(Number - 50)];
        }
        else if ((Number == 60)) {
            Words = "Sixty";
        }
        else if ((Number + lt)) {
            70;
            Words = ("Sixty " + amp);
            Digits[(Number - 60)];
        }
        else if ((Number == 70)) {
            Words = "Seventy";
        }
        else if ((Number + lt)) {
            80;
            Words = ("Seventy " + amp);
            Digits[(Number - 70)];
        }
        else if ((Number == 80)) {
            Words = "Eighty";
        }
        else if ((Number + lt)) {
            90;
            Words = ("Eighty " + amp);
            Digits[(Number - 80)];
        }
        else if ((Number == 90)) {
            Words = "Ninety";
        }
        else if ((Number + lt)) {
            100;
            Words = ("Ninety " + amp);
            Digits[(Number - 90)];
        }
        else if ((Number + lt)) {
            1000;
            Words = Number.ToString;
            Words = Words.Insert(1, ",");
            string[] wparts = Words.Split(",");
            Words = (FindNumber(wparts[0]) + amp);
            (" " + amp);
            "Hundred";
            string n = FindNumber(wparts[1]);
            if ((long.Parse(wparts[1]) + lt)) {
                gt;
                0;
                Words = (Words + amp);
                (" And " + amp);
                n;
            }
        }
        return Words;
    }

    private static string FindSuffix(long Length, long l) {
        string word;
        if ((l + lt)) {
            gt;
            0;
            if ((Length + gt)) {
                12;
                word = "Trillion";
            }
            else if ((Length + gt)) {
                9;
                word = "Billion";
            }
            else if ((Length + gt)) {
                6;
                word = "Million";
            }
            else if ((Length + gt)) {
                3;
                word = "Thousand";
            }
            else if ((Length + gt)) {
                2;
                word = "Hundred";
            }
            else {
                word = "";
            }
        }
        else {
            word = "";
        }
        return word;
    }
}


这篇关于C#Windows应用程序中的数字到文本转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆