大写金额字符串生成 C#实现

思路:
  中文对金额的描述以四位为一组,
  只考虑一万亿以内的数字则每组内以千、百、十和[亿\万\元]区分各位
  连续的零按一个处理,组内最低位的零可略去
  无角无分说整,有角无分只说角,无角有分说零X分,有角有分...

 

代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace MorrisSpace
  7 {
  8     /// <summary>
  9     /// 中文金额字符串辅助类。Helper for Amount string in Chinese 
 10     /// </summary>
 11     public class AmountStringHelper
 12     {
 13         static private readonly char[] units = { '', '', '', '', '', '', '', '亿', '' };
 14         //                                        0     1     2     3     4     5     6     7    8
 15         static private readonly char[] numbers = { '', '', '', '', '', '', '', '', '', '' };
 16 
 17         /// <summary>
 18         /// 数字金额转大写金额
 19         /// </summary>
 20         /// <param name="num">金额数字</param>
 21         /// <returns>大写金额字符串</returns>
 22         public static string GetAmountInWords(double num)
 23         {
 24             double amount = Math.Round(num, 2);
 25             long integ = (int)amount;
 26             double fract = Math.Round(amount - integ, 2);
 27             if (integ.ToString().Length > 12)
 28             {
 29                 return null;
 30             }
 31             string result = "";
 32             if (fract - 0.0 != 0)
 33             {
 34                 string tempstr = fract.ToString();
 35                 if (tempstr.Length == 3)
 36                 {
 37                     result += numbers[(int)(fract * 10)];
 38                     result += units[1];
 39                 }
 40                 else
 41                 {
 42                     int frist = (int)(fract * 10);
 43                     int second = (int)(fract * 100 - frist * 10);
 44                     if (frist != 0)
 45                     {
 46                         result += numbers[frist];
 47                         result += units[1];
 48                         result += numbers[second];
 49                         result += units[0];
 50                     }
 51                     else
 52                     {
 53                         result += numbers[0];
 54                         result += numbers[second];
 55                         result += units[0];
 56                     }
 57                 }
 58             }
 59             else
 60             {
 61                 result += units[8];
 62             }
 63 
 64             for (int temp = (int)(integ % 10000), secnum = 1; temp != 0; temp = (int)(integ % 10000), secnum++)
 65             {
 66                 result = FourBitTrans(temp) + units[secnum + 4] + result;
 67                 integ /= 10000;
 68                 if (integ != 0 && temp < 1000)
 69                 {
 70                     result = numbers[0] + result;
 71                 }
 72             }
 73             return result;
 74         }
 75         
 76         /// <summary>
 77         /// 进行四位数字转换的辅助函数
 78         /// </summary>
 79         /// <param name="num">四位以下数字</param>
 80         /// <returns>大写金额四位节</returns>
 81         public static string FourBitTrans(int num)
 82         {
 83             string tempstr = num.ToString();
 84             if (tempstr.Length > 4)
 85             {
 86                 return null;
 87             }
 88             string result = string.Empty;
 89             int i = tempstr.Length;
 90             int j = 0;
 91             bool zeromark = true;
 92             while (--i >= 0)
 93             {
 94                 j++;
 95 
 96                 if (tempstr[i] == '0')
 97                 {
 98                     if (zeromark == true)
 99                     {
100                         continue;
101                     }
102                     zeromark = true;
103                     result = numbers[0] + result;
104                     continue;
105                 }
106                 zeromark = false;
107                 if (j > 1)
108                 {
109                     result = units[j] + result;
110                 }
111                 int temp = tempstr[i] - '0';
112                 result = numbers[temp] + result;
113             }
114             return result;
115         }
116 
117         
118     }
119 }

--------------------------------------

这个代码只适合一亿以内的金额,但相信以满足绝大多数情况

posted on 2014-03-29 22:00  编号2784  阅读(571)  评论(0编辑  收藏  举报

导航