excel 数字转换为英文

  public class NumToText
        {
            private static string[] _ones =
            {
                "ZERO",
                "ONE",
                "TWO",
                "THREE",
                "FOUR",
                "FIVE",
                "SIX",
                "SEVEN",
                "EIGHT",
                "NINE"
            };

            private static string[] _teens =
            {
                "TEN",
                "ELEVEN",
                "TWELVE",
                "THIRTEEN",
                "FOURTEEN",
                "FIFTEEN",
                "SIXTEEN",
                "SEVENTEEN",
                "EIGHTEEN",
                "NINETEEN"
            };

            private static string[] _tens =
            {
                "",
                "TEN",
                "TWENTY",
                "THIRTY",
                "FORTY",
                "FIFTY",
                "SIXTY",
                "SEVENTY",
                "EIGHTY",
                "NINETY"
            };

            // US Nnumbering:
            private static string[] _thousands =
            {
                "",
                "THOUSAND",
                "MILLION",
                "BILLION",
                "TRILLION",
                "QUADRILLION"
            };

            /// <summary>
            /// Converts a numeric value to words suitable for the portion of
            /// a check that writes out the amount.
            /// </summary>
            /// <param name="value">Value to be converted</param>
            /// <returns></returns>
            public static string Convert(decimal value)
            {
                string digits, temp;
                bool showThousands = false;
                bool allZeros = true;

                // Use StringBuilder to build result
                StringBuilder builder = new StringBuilder();
                // Convert integer portion of value to string
                digits = ((long)value).ToString();
                // Traverse characters in reverse order
                for (int i = digits.Length - 1; i >= 0; i--)
                {
                    int ndigit = (int)(digits[i] - '0');
                    int column = (digits.Length - (i + 1));

                    // Determine if ones, tens, or hundreds column
                    switch (column % 3)
                    {
                        case 0:        // Ones position
                            showThousands = true;
                            if (i == 0)
                            {
                                // First digit in number (last in loop)
                                temp = String.Format("{0} ", _ones[ndigit]);
                            }
                            else if (digits[i - 1] == '1')
                            {
                                // This digit is part of "teen" value
                                temp = String.Format("{0} ", _teens[ndigit]);
                                // Skip tens position
                                i--;
                            }
                            else if (ndigit != 0)
                            {
                                // Any non-zero digit
                                temp = String.Format("{0} ", _ones[ndigit]);
                            }
                            else
                            {
                                // This digit is zero. If digit in tens and hundreds
                                // column are also zero, don't show "thousands"
                                temp = String.Empty;
                                // Test for non-zero digit in this grouping
                                if (digits[i - 1] != '0' || (i > 1 && digits[i - 2] != '0'))
                                    showThousands = true;
                                else
                                    showThousands = false;
                            }

                            // Show "thousands" if non-zero in grouping
                            if (showThousands)
                            {
                                if (column > 0)
                                {
                                    temp = String.Format("{0}{1}{2}",
                                        temp,
                                        _thousands[column / 3],
                                        allZeros ? " " : ", ");
                                }
                                // Indicate non-zero digit encountered
                                allZeros = false;
                            }
                            builder.Insert(0, temp);
                            break;

                        case 1:        // Tens column
                            if (ndigit > 0)
                            {
                                temp = String.Format("{0}{1}",
                                    _tens[ndigit],
                                    (digits[i + 1] != '0') ? "-" : " ");
                                builder.Insert(0, temp);
                            }
                            break;

                        case 2:        // Hundreds column
                            if (ndigit > 0)
                            {
                                temp = String.Format("{0} HUNDRED", _ones[ndigit]);
                                builder.Insert(0, temp);
                            }
                            break;
                    }
                }

                // Capitalize first letter
                return String.Format("{0}{1}",
                    Char.ToUpper(builder[0]),
                    builder.ToString(1, builder.Length - 1));
            }
        }
原文来自:http://www.blackbeltcoder.com/Articles/strings/converting-numbers-to-words

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值