人民币大写转化函数(ASP版和JavaScript版)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/esshs/article/details/330484

ASP 版:

<%
'文 件 名:cmycur.asp
'作    者:二十四画生
'版    本:1.0
'简    介:转换函数文件。
'功能描述:转换货币形式的函数。(该文件包含两个函数,一个转换成一般货币形式cmycur(num),一个转换成大写形式cmycurd(num))
%>
<%
function cmycur(num) '转换为货币形式如:¥180.00元
  dim thenum
  if isnull(num) or num = "" then
    cmycur = " "
  else
    num = Round(num,2)
 thenum = FormatCurrency(num,2,-1)
 cmycur = thenum & "元"
  end if
end function

function cmycurd(num) 'num为要转换成大写的金额
  dim str1  '如下定义
  dim str2  '如下定义
  dim str3  '从原num值中取出的值
  dim i   '循环变量
  dim j   'num的值乘以100的字符串长度
  dim ch1  '数字的汉语读法
  dim ch2  '数字位的汉字读法
  dim nzero  '用来计算连续的零值是几个

  str1 = "零壹贰叁肆伍陆柒捌玖"
  str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"
  nzero = 0
  if isnull(num) or num = "" then
    cmycurd = " "
    exit function
  end if
  num = Round(Abs(num),2) '将num取绝对值并四舍五入取2位小数
  j = Len(CStr(num * 100))  '找出最高位
  if j > 15 then
    cmycurd = "溢出"
    exit function
  end if
  str2 = Right(str2, j) '取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分
 
  '循环取出每一位需要转换的值
  for i = 1 to j
    str3 = Mid(num * 100, i, 1) '取出需转换的某一位的值
 if i <> (j - 3) + 1 and i <> (j - 7) + 1 and i <> (j - 11) + 1 and i <>(j - 15) + 1 then    '当所取位数不为元、万、亿、万亿上的数字时
   if str3 = 0 then
     ch1 = ""
     ch2 = ""
  nzero = nzero + 1
   elseif str3 <> 0 and nzero <> 0 then
     ch1 = "零" & Mid(str1, clng(str3) + 1, 1)
        ch2 = Mid(str2, i, 1)
        nzero = 0
      else
     ch1 = Mid(str1, CLng(str3) + 1, 1)
        ch2 = Mid(str2, i, 1)
        nzero = 0
      end if
    else '该位是万亿,亿,万,元位等关键位
      if str3 <> 0 and nzero <> 0 then
        ch1 = "零" & Mid(str1, clng(str3) + 1, 1)
        ch2 = Mid(str2, i, 1)
        nzero = 0
      elseif str3 <> 0 and nzero = 0 Then
        ch1 = Mid(str1, clng(str3) + 1, 1)
        ch2 = Mid(str2, i, 1)
        nzero = 0
      elseif str3 = 0 and nzero >= 3 Then
        ch1 = ""
        ch2 = ""
        nzero = nzero + 1
      elseif j > 10 Then  '当为整亿以上的时候,会多添加一个万,因此把这种情况去掉
        ch1 = ""
        nzero = nzero + 1
   else
     ch1 = ""
  ch2 = Mid(str2, i, 1)
  nzero = nzero + 1
      end if
      if i = (j - 11) + 1 Or i = (j - 3) + 1 Then '如果该位是亿位或元位,则必须写上
        ch2 = Mid(str2, i, 1)
      end if
    end if

    cmycurd = cmycurd & ch1 & ch2

    if i = j and str3 = 0 Then '最后一位(分)为0时,加上“整”
      cmycurd = cmycurd & "整"
    end if
  next

  if num = 0 then
    cmycurd="零元整"
  end if
end function
%>

JavaScript版:

function cmycurd(num){  //转成人民币大写金额形式
  var str1 = '零壹贰叁肆伍陆柒捌玖';  //0-9所对应的汉字
  var str2 = '万仟佰拾亿仟佰拾万仟佰拾元角分'; //数字位所对应的汉字
  var str3;    //从原num值中取出的值
  var str4;    //数字的字符串形式
  var str5 = '';  //人民币大写金额形式
  var i;    //循环变量
  var j;    //num的值乘以100的字符串长度
  var ch1;    //数字的汉语读法
  var ch2;    //数字位的汉字读法
  var nzero = 0;  //用来计算连续的零值是几个
 
  num = Math.abs(num).toFixed(2);  //将num取绝对值并四舍五入取2位小数
  str4 = (num * 100).toFixed(0).toString();  //将num乘100并转换成字符串形式
  j = str4.length;      //找出最高位
  if (j > 15){return '溢出';}
  str2 = str2.substr(15-j);    //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分
 
  //循环取出每一位需要转换的值
  for(i=0;i<j;i++){
    str3 = str4.substr(i,1);   //取出需转换的某一位的值
    if (i != (j-3) && i != (j-7) && i != (j-11) && i != (j-15)){    //当所取位数不为元、万、亿、万亿上的数字时
   if (str3 == '0'){
     ch1 = '';
     ch2 = '';
  nzero = nzero + 1;
   }
   else{
     if(str3 != '0' && nzero != 0){
       ch1 = '零' + str1.substr(str3*1,1);
          ch2 = str2.substr(i,1);
          nzero = 0;
  }
  else{
    ch1 = str1.substr(str3*1,1);
          ch2 = str2.substr(i,1);
          nzero = 0;
        }
   }
 }
 else{ //该位是万亿,亿,万,元位等关键位
      if (str3 != '0' && nzero != 0){
        ch1 = "零" + str1.substr(str3*1,1);
        ch2 = str2.substr(i,1);
        nzero = 0;
      }
      else{
     if (str3 != '0' && nzero == 0){
          ch1 = str1.substr(str3*1,1);
          ch2 = str2.substr(i,1);
          nzero = 0;
  }
        else{
    if (str3 == '0' && nzero >= 3){
            ch1 = '';
            ch2 = '';
            nzero = nzero + 1;
       }
       else{
      if (j >= 11){
              ch1 = '';
              nzero = nzero + 1;
   }
   else{
     ch1 = '';
     ch2 = str2.substr(i,1);
     nzero = nzero + 1;
   }
          }
  }
   }
 }
    if (i == (j-11) || i == (j-3)){  //如果该位是亿位或元位,则必须写上
        ch2 = str2.substr(i,1);
    }
    str5 = str5 + ch1 + ch2;
   
    if (i == j-1 && str3 == '0' ){   //最后一位(分)为0时,加上“整”
      str5 = str5 + '整';
    }
  }
  if (num == 0){
    str5 = '零元整';
  }
  return str5;
}

还有一个C#版的没有贴出来,原理差不多,大家自己写吧!

展开阅读全文

数值转换为人民币大写函数代码

12-23

根据"lihonggen0(李洪根,MS MVP,标准答案来了) "发的VB代码,修改了一下改为PB脚本,在PB8下调试通过.发出来给大家分享rnrn//rn//'*********************************************************rn//'* 名称:nNumber2Chinesern//'* 功能:数值转换为人民币(汉字)rn//'* 用法:nNumber2Chinese(数值)rn//'*********************************************************rn//'参数 txtje 为 double 类型rn//'返回值 Num2Chi 为 string 类型rninteger I, K rnString NC, nd, ka, chrNum, strZheng rnString c1, c2, c3 rnInteger K1 rnString Zheng,Num2ChirnString Xiaornrn NC = Trim(string(txtJE,'##.00'))rn c1 = "仟佰拾万仟佰拾亿仟佰拾万仟佰拾元"rn c2 = "角分"rn c3 = "玖捌柒陆伍肆叁贰壹"rn If double(NC) = 0 Thenrn Num2Chi = "零元整"rn return Num2Chirn End Ifrn Num2Chi = ""rn Zheng = Mid(NC, 1, (Len(NC) - 3))rn Xiao = Mid(NC, (Len(Zheng) + 2))rn If integer(Xiao) <> 0 Thenrn For I = Len(Xiao) To 1 Step -1rn chrNum = Mid(Xiao, I, 1)rn If integer(chrNum) <> 0 Thenrn Num2Chi = Mid(c2, I, 1) + Num2Chirn Num2Chi = Mid(c3, (lenw(c3) - integer(chrNum) + 1), 1) + Num2Chirn End Ifrn Next rn End Ifrn rn K = 0rn If integer(Zheng) <> 0 Thenrn Num2Chi = "元" + Num2Chirn For I = Len(Zheng) To 1 Step -1rn If (Len(Zheng) - I) = 4 Thenrn Num2Chi = "万" + Num2Chirn ElseIf (Len(Zheng) - I) = 8 Thenrn Num2Chi = "亿" + Num2Chirn ElseIf (Len(Zheng) - I) = 12 Thenrn Num2Chi = "万" + Num2Chirn End Ifrn chrNum = Mid(Zheng, I, 1)rn If integer(chrNum) <> 0 Thenrn If I = Len(Zheng) Thenrn Num2Chi = Mid(c3, (lenw(c3) - integer(chrNum) + 1), 1) + Num2Chirn Elsern If (Len(Zheng) - I) <> 4 And (Len(Zheng) - I) <> 8 And (Len(Zheng) - I) <> 12 Thenrn Num2Chi = Mid(c1, (lenw(c1) - K), 1) + Num2Chirn End Ifrn Num2Chi = Mid(c3, (lenw(c3) - integer(chrNum) + 1), 1) + Num2Chirn End Ifrn Elsern If Mid(Num2Chi, 1, 1) <> "元" And Mid(Num2Chi, 1, 1) <> "万" And Mid(Num2Chi, 1, 1) <> "亿" Thenrn If Mid(Num2Chi, 1, 1) <> "零" Thenrn Num2Chi = "零" + Num2Chirn End Ifrn End Ifrn End Ifrn K = K + 1rn Next rn End Ifrn If Right(Trim(Num2Chi), 1) <> "分" Thenrn Num2Chi = Num2Chi + "整"rn End Ifrnrnrnrnreturn Num2Chirn 论坛

没有更多推荐了,返回首页