ChineseMoney.cs
上传用户:jx_fiona
上传日期:2014-03-08
资源大小:1387k
文件大小:7k
源码类别:

打印编程

开发平台:

Others

  1. using System;
  2. namespace GoldPrinter
  3. {
  4. /// <summary>
  5. /// 本类实现阿拉伯数字到大写中文的转换
  6. /// 该类没有对非法数字进行判别
  7. /// </summary>
  8. public class ChineseNum
  9. {
  10. public static string GetChineseNum(string p_num)
  11. {
  12. ChineseNum cn = new ChineseNum();
  13. return cn.NumToChn(p_num);
  14. }
  15. public static string GetUpperMoney(double p_Money)
  16. {
  17. ChineseNum cn = new ChineseNum();
  18. return cn.GetMoneyChinese(p_Money);
  19. }
  20. //转换数字
  21. private char CharToNum(char x)
  22. {
  23. string stringChnNames="零一二三四五六七八九";
  24. string stringNumNames="0123456789";
  25. return stringChnNames[stringNumNames.IndexOf(x)];
  26. }
  27. //转换万以下整数
  28. private string WanStrToInt(string x)
  29. {
  30. string[] stringArrayLevelNames=new string[4] {"","十","百","千"};
  31. string ret="";
  32. int i;
  33. for (i=x.Length-1;i>=0;i--)
  34. if (x[i]=='0')
  35. ret=CharToNum(x[i])+ret;
  36. else
  37. ret=CharToNum(x[i])+stringArrayLevelNames[x.Length-1-i]+ret;
  38. while ((i=ret.IndexOf("零零"))!=-1)
  39. ret=ret.Remove(i,1);
  40. if (ret[ret.Length-1]=='零' && ret.Length>1)
  41. ret=ret.Remove(ret.Length-1,1);
  42. if (ret.Length>=2 && ret.Substring(0,2)=="一十")
  43. ret=ret.Remove(0,1);
  44. return ret;
  45. }
  46. //转换整数
  47. private string StrToInt(string x)
  48. {
  49. int len=x.Length;
  50. string ret,temp;
  51. if (len<=4)
  52. ret=WanStrToInt(x);
  53. else if (len<=8)
  54. {
  55. ret=WanStrToInt(x.Substring(0,len-4))+"万";
  56. temp=WanStrToInt(x.Substring(len-4,4));
  57. if (temp.IndexOf("千")==-1 && temp!="")
  58. ret+="零"+temp;
  59. else
  60. ret+=temp;
  61. }
  62. else
  63. {
  64. ret=WanStrToInt(x.Substring(0,len-8))+"亿";
  65. temp=WanStrToInt(x.Substring(len-8,4));
  66. if (temp.IndexOf("千")==-1 && temp!="")
  67. ret+="零"+temp;
  68. else
  69. ret+=temp;
  70. ret+="万";
  71. temp=WanStrToInt(x.Substring(len-4,4));
  72. if (temp.IndexOf("千")==-1 && temp!="")
  73. ret+="零"+temp;
  74. else
  75. ret+=temp;
  76. }
  77. int i;
  78. if ((i=ret.IndexOf("零万"))!=-1)
  79. ret=ret.Remove(i+1,1);
  80. while ((i=ret.IndexOf("零零"))!=-1)
  81. ret=ret.Remove(i,1);
  82. if (ret[ret.Length-1]=='零' && ret.Length>1)
  83. ret=ret.Remove(ret.Length-1,1);
  84. return ret;
  85. }
  86. //转换小数
  87. private string StrToDouble(string x)
  88. {
  89. string ret="";
  90. for (int i=0;i<x.Length;i++)
  91. ret+=CharToNum(x[i]);
  92. return ret;
  93. }
  94. private string NumToChn(string x)
  95. {
  96. if (x.Length==0)
  97. return "";
  98. string ret="";
  99. if (x[0]=='-')
  100. {
  101. ret="负";
  102. x=x.Remove(0,1);
  103. }
  104. if (x[0].ToString()==".")
  105. x="0"+x;
  106. if (x[x.Length-1].ToString()==".")
  107. x=x.Remove(x.Length-1,1);
  108. if (x.IndexOf(".")>-1)
  109. ret+=StrToInt(x.Substring(0,x.IndexOf(".")))+"点"+StrToDouble(x.Substring(x.IndexOf(".")+1));
  110. else
  111. ret+=StrToInt(x);
  112. return ret;
  113. }
  114. private string GetMoneyChinese(Double Money) 
  115. {
  116. int i;
  117. string mstrSource;
  118. if (Money == 0) {return "";}
  119. mstrSource = Money.ToString("#0.00");
  120. i = mstrSource.IndexOf(".");
  121. if (i > 0) {mstrSource = mstrSource.Replace(".","");}
  122. if (mstrSource.Substring(0,1) == "0") {mstrSource = mstrSource.Remove(0,1);}
  123.  
  124. mstrSource = NumstrToChinese(mstrSource);
  125. if (mstrSource.Length == 0) {return "";}
  126. //负
  127. if (Money < 0) {mstrSource = "负" + mstrSource;}       
  128.  
  129. mstrSource=mstrSource.Replace("0","零");
  130. mstrSource=mstrSource.Replace("1","壹");
  131. mstrSource=mstrSource.Replace("2","贰");
  132. mstrSource=mstrSource.Replace("3","叁");
  133. mstrSource=mstrSource.Replace("4","肆");
  134. mstrSource=mstrSource.Replace("5","伍");
  135. mstrSource=mstrSource.Replace("6","陆");
  136. mstrSource=mstrSource.Replace("7","柒");
  137. mstrSource=mstrSource.Replace("8","捌");
  138. mstrSource=mstrSource.Replace("9","玖");
  139. mstrSource=mstrSource.Replace("M","亿");
  140. mstrSource=mstrSource.Replace("W","万");
  141. mstrSource=mstrSource.Replace("S","仟");
  142. mstrSource=mstrSource.Replace("H","佰");
  143. mstrSource=mstrSource.Replace("T","拾");
  144. mstrSource=mstrSource.Replace("Y","圆");
  145. mstrSource=mstrSource.Replace("J","角");
  146. mstrSource=mstrSource.Replace("F","分");
  147. if (mstrSource.Substring(mstrSource.Length-1, 1) != "分") {mstrSource = mstrSource + "整";}
  148. return mstrSource;
  149. }
  150. //金额转换
  151. private string NumstrToChinese(string numstr) 
  152. {
  153. int i;
  154. int j;
  155. string mstrChar;
  156. string[] mstrFlag=new string[4];
  157. string mstrReturn="";
  158. bool mblnAddzero=false;
  159. mstrFlag[0] = "";
  160. mstrFlag[1] = "T";
  161. mstrFlag[2] = "H";
  162. mstrFlag[3] = "S";
  163.     
  164. for (i = 1;i<=numstr.Length;i++) 
  165. {
  166. j = numstr.Length  - i;
  167. mstrChar = numstr.Substring(i-1,1); 
  168. if (mstrChar != "0" && j > 1) {mstrReturn = mstrReturn + mstrChar + mstrFlag[(j - 2) % 4];}
  169. if (mstrChar == "0" && mblnAddzero==false)
  170. {
  171. mstrReturn = mstrReturn + "0";
  172. mblnAddzero = true;
  173. }
  174. if (j == 14)
  175. {
  176. if (mstrReturn.Substring(mstrReturn.Length-1) == "0") 
  177. {mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "W0";}
  178. else
  179. {mstrReturn = mstrReturn + "W";}
  180. }
  181. if (j == 2) 
  182. {
  183. if (mstrReturn.Substring(mstrReturn.Length-1,1) == "0")
  184. {mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "Y0";}
  185. else
  186. {mstrReturn = mstrReturn + "Y";}
  187. //元
  188. }
  189. if (j == 6)
  190. {
  191. if (mstrReturn.Length  > 2) 
  192. {
  193. if (mstrReturn.Substring(mstrReturn.Length-2)  != "M0") 
  194. {
  195. if (mstrReturn.Substring(mstrReturn.Length-1) == "0")
  196. {mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "W0";}
  197. else
  198. {mstrReturn = mstrReturn + "W";}
  199. }
  200. }
  201. else
  202. {
  203. if (mstrReturn.Substring(mstrReturn.Length-1) == "0")
  204. {mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "W0";}
  205. else
  206. {mstrReturn = mstrReturn + "W";}
  207. }
  208. }
  209. if (j == 10)
  210. {
  211. if (mstrReturn.Substring(mstrReturn.Length-1) == "0") 
  212. {mstrReturn =mstrReturn.Substring(0,mstrReturn.Length-1) + "M0";}
  213. else
  214. {mstrReturn = mstrReturn + "M";}
  215. }
  216. if (j == 0 && mstrChar != "0") {mstrReturn = mstrReturn + mstrChar + "F";}
  217. if (j == 1 && mstrChar != "0") {mstrReturn = mstrReturn + mstrChar + "J";}
  218. if (mstrChar != "0") {mblnAddzero = false;}
  219. }
  220. if (mstrReturn.Substring(0, 1) == "1" && mstrReturn.Substring(1, 1) == mstrFlag[1]) {mstrReturn = mstrReturn.Substring(1);}
  221. if (mstrReturn.Substring(mstrReturn.Length-1, 1) == "0"){mstrReturn = mstrReturn.Substring(0,mstrReturn.Length-1);}
  222. if (mstrReturn.Substring(0, 1) == "0") {mstrReturn = mstrReturn.Substring(1);}
  223. if (mstrReturn.Substring(mstrReturn.Length-1, 1) == "M" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "W" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "S" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "H" || mstrReturn.Substring(mstrReturn.Length-1, 1) == "T") {mstrReturn = mstrReturn + "Y";}
  224. return mstrReturn;
  225. }
  226. }
  227. }