随心所欲

做个幸福的人
posts - 147, comments - 1402, trackbacks - 28, articles - 0
  博客园 :: 首页 :: 新随笔 ::  :: 订阅 订阅 :: 管理

数字转换成货币字符串的程序

Posted on 2006-12-01 12:36 随心所欲 阅读(413) 评论(2)  编辑 收藏 网摘 所属分类: 其他技术

 

这是一个可以把数字转换成货币字符串的程序。

在一些情况下,需要把12.50转化成“Twelve Dollars Fifty Cents”的现实方式,这个程序就是做这样一个转化的。

它可以做一些扩展,比方说做成转化成数字(Twelve point five),汉字(壹十贰圆伍角整)等等

http://www.osix.net/modules/article/?id=46

一开始在网上找到了这个例子,但是不能直接运行,错误很多,好像不全。于是自己改写了一下。难度没有,就是一个实用的小工具。和以前的那个可以转化汉语拼音的差不多。

 

  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Web;
  5using System.Web.Security;
  6using System.Web.UI;
  7using System.Web.UI.WebControls;
  8using System.Web.UI.WebControls.WebParts;
  9using System.Web.UI.HtmlControls;
 10
 11/// <summary>
 12/// Summary description for Number2Text
 13/// Number to Currency
 14/// </summary>

 15public class Number2Currency
 16{
 17    public Number2Currency()
 18    {
 19 
 20    }

 21    private int Fra, Hun, Tho, Mil, Bil;//.xx,100;1000;1000,000;1000,000,000
 22    private String[] Ones = new String[] """One""Two""Three""Four""Five""Six""Seven""Eight""Nine" };
 23    private String[] Teens = new String[] "Ten""Eleven""Twelve""Thirteen""Fourteen""Fifteen""Sixteen""Seventeen""Eightteen""Nineteen" };
 24    private String[] Tys = new String[] """""Twenty""Thirty""Forty""Fifty""Sixty""Seventy""Eighty""Ninety" };
 25
 26    /// <summary>
 27    /// divide every 3 dig. xx,xxx,xxx
 28    /// </summary>
 29    /// <param name="strn1"></param>

 30    private void Divide(string strNumber)
 31    {
 32        if (strNumber.IndexOf("."> -1)
 33        {
 34            string strFra = strNumber.Substring(strNumber.IndexOf(".")+1);//just 2 dig, for cents
 35            if (strFra.Length == 1)
 36                strFra += "0";
 37            else if (strFra.Length >= 2)
 38                strFra = strFra.Substring(02);
 39            else 
 40                strFra = "0";
 41            Fra= Int32.Parse(strFra);
 42            strNumber= strNumber.Substring(0,strNumber.IndexOf("."));//get left
 43        }

 44        int len = strNumber.Length;
 45        switch (len)
 46        {
 47            case 1:
 48                goto case 3;
 49            case 2:
 50                goto case 3;
 51            case 3:
 52                Hun = Int32.Parse(strNumber);
 53                break;
 54            case 4:
 55                goto case 6;
 56            case 5:
 57                goto case 6;
 58            case 6:
 59                Hun = Int32.Parse(strNumber.Substring(len - 33));
 60                Tho = Int32.Parse(strNumber.Substring(0, len - 3));
 61                break;
 62            case 7:
 63                goto case 9;
 64            case 8:
 65                goto case 9;
 66            case 9:
 67                Hun = Int32.Parse(strNumber.Substring(len - 33));
 68                Tho = Int32.Parse(strNumber.Substring(len - 63));
 69                Mil = Int32.Parse(strNumber.Substring(0, len - 6));
 70                break;
 71            case 10:
 72                goto case 12;
 73            case 11:
 74                goto case 12;
 75            case 12:
 76                Hun = Int32.Parse(strNumber.Substring(len - 33));
 77                Tho = Int32.Parse(strNumber.Substring(len - 63));
 78                Mil = Int32.Parse(strNumber.Substring(len - 93));
 79                Bil = Int32.Parse(strNumber.Substring(0, len - 9));
 80                break;
 81        }

 82    }

 83
 84
 85
 86    /// <summary>
 87    ///  number less than 1000
 88    /// </summary>
 89    /// <param name="number"></param>
 90    /// <returns></returns>

 91    private string SplitDigits(int number)
 92    {
 93        string strH = "", strT = "";
 94
 95        int hun1 = 0, ten1 = 0, one1 = 0;//set 0 hur 0 tens and 0
 96
 97        hun1 = number / 100;//get how much hundreds *******
 98        ten1 = (number % 100/ 10;// 
 99        one1 = (number % 100% 10;
100
101        if (hun1 > 0)//[1-9][00-99]
102        {
103            strH = Ones[hun1] + " Hundred ";
104        }

105        else if (hun1 == 0)//<100
106        {
107            strH = Ones[hun1];//not a hundred, then use " ".Ones[0]=" "  
108        }

109        if (ten1 > 1)//[2-9][0-9]
110        {
111            strT = Tys[ten1] + " " + Ones[one1];
112        }

113        else if (ten1 == 1)//11,12 no need one1s
114        {
115            strT = Teens[one1];//***** use number
116        }

117        else //ten1=0
118        {
119            strT = Ones[one1];
120        }

121        return strH + strT;
122
123    }

124    public string NumberToCurrency(double currency)
125    {
126        Divide(currency.ToString());//divide first
127
128        string strHun = "", strTho = "", strMil = "", strBil = "", strFra = "";
129        if (Fra >0)
130        {
131            strFra = " " + SplitDigits(Fra)+" Cents";
132        }

133        if(Hun>0)
134        {
135            strHun = SplitDigits(Hun) + " Dollars";
136        }

137        if (Tho > 0)
138        {
139            strTho = SplitDigits(Tho) + " Thousand and ";
140        }

141        if (Mil > 0)
142        {
143            strMil = SplitDigits(Mil) + " Million and ";
144        }

145        if (Bil > 0)
146        {
147            strBil = SplitDigits(Bil) + " Billion and ";
148        }

149
150        return (strBil + strMil + strTho + strHun + strFra);
151
152    }

153}

Feedback

#1楼    回复  引用    

2006-12-02 10:52 by 木 [未注册用户]
要汉字的

#2楼 [楼主]   回复  引用  查看    

2006-12-04 17:40 by 随心所欲      
@木
sorry, i do not have now




标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》

相关文章:

相关链接: