
JS实现金额转换(将输入的阿拉伯数字)转换成中文
发布日期:2021-05-09 00:48:40
浏览次数:18
分类:博客文章
本文共 2224 字,大约阅读时间需要 7 分钟。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script type="text/javascript" language="javascript">function clickchange(obj){var aaval=obj.value;alert(aaval);document.getElementById("bb").value=ChinaCost(aaval);}function ChinaCost(numberValue){
var numberValue=new String(Math.round(numberValue*100)); // 数字金额var chineseValue=""; // 转换后的汉字金额var String1 = "零壹贰叁肆伍陆柒捌玖"; // 汉字数字var String2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; // 对应单位var len=numberValue.length; // numberValue 的字符串长度var Ch1; // 数字的汉语读法var Ch2; // 数字位的汉字读法var nZero=0; // 用来计算连续的零值的个数var String3; // 指定位置的数值if(len>15){alert("超出计算范围");return "";}if (numberValue==0){chineseValue = "零元整";return chineseValue;}String2 = String2.substr(String2.length-len, len); // 取出对应位数的STRING2的值
for(var i=0; i<len; i++){String3 = parseInt(numberValue.substr(i, 1),10); // 取出需转换的某一位的值if ( i != (len - 3) && i != (len - 7) && i != (len - 11) && i !=(len - 15) ){if ( String3 == 0 ){Ch1 = "";Ch2 = "";nZero = nZero + 1;}else if ( String3 != 0 && nZero != 0 ){Ch1 = "零" + String1.substr(String3, 1);Ch2 = String2.substr(i, 1);nZero = 0;}else{Ch1 = String1.substr(String3, 1);Ch2 = String2.substr(i, 1);nZero = 0;}}else{ // 该位是万亿,亿,万,元位等关键位if( String3 != 0 && nZero != 0 ){Ch1 = "零" + String1.substr(String3, 1);Ch2 = String2.substr(i, 1);nZero = 0;}else if ( String3 != 0 && nZero == 0 ){Ch1 = String1.substr(String3, 1);Ch2 = String2.substr(i, 1);nZero = 0;}else if( String3 == 0 && nZero >= 3 ){Ch1 = "";Ch2 = "";nZero = nZero + 1;}else{Ch1 = "";Ch2 = String2.substr(i, 1);nZero = nZero + 1;}if( i == (len - 11) || i == (len - 3)){ // 如果该位是亿位或元位,则必须写上Ch2 = String2.substr(i, 1);}}chineseValue = chineseValue + Ch1 + Ch2;}if ( String3 == 0 ){ // 最后一位(分)为0时,加上“整”
chineseValue = chineseValue + "整";}return chineseValue;
}</script></head><body>
<input type="text" id="aa" onblur="clickchange(this)" /><input type="text" id="bb" /></body></html>
转载自:https://www.cnblogs.com/yuxia/p/3347173.html
仅供个人学习使用
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2025年04月24日 05时18分31秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
pair的用法
2019-03-09
Flex 布局的自适应子项内容过长导致其被撑大问题
2019-03-09
PL/SQL 动态Sql拼接where条件
2019-03-09
Thymeleaf sec:authorize 标签不生效
2019-03-11
Boostrap技能点整理之【网格系统】
2019-03-11
javaWeb服务详解(含源代码,测试通过,注释) ——Emp的Dao层
2019-03-11
echarts 基本图表开发小结
2019-03-11
JDK9-15新特性
2019-03-11
TreeSet、TreeMap
2019-03-11
JVM内存模型
2019-03-11
可变长度参数
2019-03-11
cordova打包apk更改图标
2019-03-11
GitHub上传时,项目在已有文档时直接push出现错误解决方案
2019-03-11
文件系统的层次结构
2019-03-11
vue(渐进式前端框架)
2019-03-11
嵌入式系统试题库(CSU)
2019-03-12
Form窗体属性
2019-03-12
vue 错误收集
2019-03-12