javascipt 金额大小写转换的实现代码

发布时间:2020-12-19编辑:脚本学堂
本文介绍一个对金额大小写进行转换的js代码,有需要的朋友,可以参考下。

1、自定义函数实现金额大小写的转换
 

复制代码 代码示例:
<html>
<body>
<script type="text/javascript">
/**
 * 金额大小写转换
 * Edit www.jb200.com
*/
var stmp = "";
function nst(t)
{
  if(t.value==stmp) return;
  var ms = t.value.replace(/[^d.]/g,"").replace(/([url=file://%20%20.%20%20d%7b2%7d).+$/,%22$1%22).replace(/%5E0+([1-9])/,%22$1%22).replace(/%5E0+$/,%220].d{2}).+$/,"$1").replace(/^0+([1-9])/,"$1").replace(/^0+$/,"0");
  var ms = t.value;
  var txt = ms.split(".");
  while(/d{4}(,|$)/.test(txt[0]))
  txt[0] = txt[0].replace(/([url=file://%20%20d)(%20%20d%7b3%7d(,%7c$))/,%22$1,$2]d)(d{3}(,|$))/,"$1,$2");
  t.value = stmp = txt[0]+(txt.length>1?"."+txt[1]:"");
  bbb.value = number2num1(ms-0);
}
function number2num1(strg)
{
  var number = Math.round(strg*100)/100;
  number = number.toString(10).split(".");
  var a = number[0];
  if (a.length > 12)
  return "数值超出范围!支持的最大数值为 999999999999.99";
  var e = "零壹贰叁肆伍陆柒捌玖";
  var num1 = "";
  var len = a.length-1;
  for (var i=0 ; i<=len; i++)
  num1 += e.charAt(parseInt(a.charAt(i))) + [["圆","万","亿"][Math.floor((len-i)/4)],"拾","佰","仟"][(len-i)%4];
  if(number.length==2 && number[1]!="")
  {
  var a = number[1];
  for (var i=0 ; i<a.length; i++)
  num1 += e.charAt(parseInt(a.charAt(i))) + ["角","分"];
  }
  num1 = num1.replace(/零佰|零拾|零仟|零角/g,"零");
  num1 = num1.replace(/零{2,}/g,"零");
  num1 = num1.replace(/零(?=圆|万|亿)/g,"");
  num1 = num1.replace(/亿万/,"亿");
  num1 = num1.replace(/^圆零?/,"");
  if(num1!="" && !/分$/.test(num1))
  num1 += "整";
  return num1;
}
</script>
小写金额:<input type="text" name="aaa" onkeyup="nst(this)"><br>
大写金额:<input type="text" name="bbb" size=80>
</body>
</html>

2、js自带的转换英文字符大小写的函数
 

复制代码 代码示例:
<script language="javascript">
var stmp1 = "Hello World";
alert(stmp1.toLocaleUpperCase());//转换成大写
alert(stmp1.toUpperCase())//转换成大写
alert(stmp1.toLocaleLowerCase());//转换成小写
alert(stmp1.toLowerCase())//转换成小写
</script>