JS实现人民币小写转换成汉字大写

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 17:46   114   0

源代码下载地址:http://download.csdn.net/detail/afgasdg/5675587

一、效果图:


二、JS代码

[javascript]view plaincopy
  1. /**
  2. *将人民币数字转换成汉字大写,并为ID为upperInputId的Input设置
  3. *@paramlowerInputId小写人民币InputId号
  4. *@paramupperInputId大写人民币InputId号
  5. */
  6. functiontoUpper(lowerInputId,upperInputId){
  7. varnum=$("#"+lowerInputId).val();
  8. if(isNull(num)){
  9. return;
  10. }
  11. if(num.lastIndexOf(".")==num.length-1){
  12. return;
  13. }
  14. for(varint=0;int<num.length;int++){
  15. if(checkNum(num)){
  16. break;
  17. }else{
  18. num=removeLastChar(num);
  19. int--;
  20. }
  21. if(isNull(num))return;
  22. }
  23. $("#"+lowerInputId).val(num);
  24. $("#"+upperInputId).val(ToTrans(num));
  25. functionisNull(num){
  26. if(num==null||num==""){
  27. $("#"+lowerInputId).val("");
  28. $("#"+upperInputId).val("");
  29. returntrue;
  30. }
  31. returnfalse;
  32. }
  33. }
  34. //校验是否为正浮点数或正整数
  35. functioncheckNum(str){
  36. varpatrn=/^([+]?)\d*\.?\d+$/;
  37. returnpatrn.test(str);
  38. };
  39. //移除最后一个字符
  40. functionremoveLastChar(str){
  41. if(str==null||str==""){
  42. returnstr;
  43. }
  44. returnstr.substring(0,str.length-1);
  45. }
  46. functionToTrans(a){
  47. varb=9.999999999999E10,
  48. f="\u96f6",
  49. h="\u58f9",
  50. g="\u8d30",
  51. e="\u53c1",
  52. k="\u8086",
  53. p="\u4f0d",
  54. q="\u9646",
  55. r="\u67d2",
  56. s="\u634c",
  57. t="\u7396",
  58. l="\u62fe",
  59. d="\u4f70",
  60. i="\u4edf",
  61. m="\u4e07",
  62. j="\u4ebf",
  63. u="人民币",
  64. o="\u5143",
  65. c="\u89d2",
  66. n="\u5206",
  67. v="\u6574";
  68. a=a.toString();
  69. if(a==""){
  70. alert("转换内容不能为空!");
  71. return"";
  72. }
  73. if(a.match(/[^,.\d]/)!=null){
  74. alert("输入有误,请输入小数点和纯数字!");
  75. return"";
  76. }
  77. if(a.match(/^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$/)==null){
  78. alert("输入有误,请输入小数点和纯数字!");
  79. return"";
  80. }
  81. a=a.replace(/,/g,"");
  82. a=a.replace(/^0+/,"");
  83. if(Number(a)>b){
  84. alert("\u5bf9\u4e0d\u8d77,\u4f60\u8f93\u5165\u7684\u6570\u5b57\u592a\u5927\u4e86!\u6700\u5927\u6570\u5b57\u4e3a99999999999.99\uff01");
  85. return"";
  86. }
  87. b=a.split(".");
  88. if(b.length>1){
  89. a=b[0];
  90. b=b[1];
  91. b=b.substr(0,2);
  92. }else{
  93. a=b[0];
  94. b="";
  95. }
  96. h=newArray(f,h,g,e,k,p,q,r,s,t);
  97. l=newArray("",l,d,i);
  98. m=newArray("",m,j);
  99. n=newArray(c,n);
  100. c="";
  101. if(Number(a)>0){
  102. for(d=j=0;d<a.length;d++){
  103. e=a.length-d-1;
  104. i=a.substr(d,1);
  105. g=e/4;
  106. e=e%4;
  107. if(i=="0")j++;
  108. else{
  109. if(j>0)c+=h[0];
  110. j=0;
  111. c+=h[Number(i)]+l[e];
  112. }
  113. if(e==0&&j<4)c+=m[g];
  114. }
  115. c+=o;
  116. }
  117. if(b!="")for(d=0;d<b.length;d++){
  118. i=b.substr(d,1);
  119. if(i!="0")c+=h[Number(i)]+n[d];
  120. }
  121. if(c=="")c=f+o;
  122. if(b.length<2)c+=v;
  123. returnc;
  124. }

三、HTML代码:

  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>人民币小写转大写</title>
  5. <scripttype="text/javascript"src="jquery-1.4.2.min.js"></script>
  6. <scripttype="text/javascript"src="renMinBi.js"></script>
  7. <scripttype="text/javascript">
  8. $(function(){
  9. $("#lower").keyup(function(){
  10. toUpper("lower","upper");
  11. });
  12. });
  13. </script>
  14. </head>
  15. <body>
  16. <h2>人民币小写转大写JS实现</h2>
  17. 人民币小写:<inputtype="text"id="lower"style="width:150px;"/>
  18. <br>
  19. 人民币大写:<inputtype="text"id="upper"style="width:400px"disabled="disabled"/>
  20. </body>
  21. </html>

源代码下载地址:http://download.csdn.net/detail/afgasdg/5675587
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP