js数字转换为大写(包含负数)
需要一段数字转换为大写的js,网上找的都不包含负数的转换,求大神帮忙
//小写转大写
function ToDX(n) {
if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) return "数据非法";
var unit = "仟佰拾亿仟佰拾万仟佰拾点角分",
str = "";
n += "00";
var p = n.indexOf('.');
if (p >= 0) n = n.substring(0, p) + n.substr(p + 1, 2);
unit = unit.substr(unit.length - n.length);
for (var i = 0; i < n.length; i++) str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
str = str.replace(/零(仟|佰|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|点)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^点零?|零分/g, "").replace(/点$/g, "").replace(/角|分/g, "") + "千克";
if (str == "千克") return "零千克";
else return str;
}
//小写转大写
function ToDX(n) {
if (!/^(0
[解决办法]
\-*\d*)(\.\d+)?$/.test(n)) return "数据非法";
var unit = "仟佰拾亿仟佰拾万仟佰拾点角分",
str = (n.toString().indexOf("-")==0?"负":"");
n =(n.toString().indexOf("-")==0?n.toString().substring(1,n.toString().length):n)+ "00";
var p = n.indexOf('.');
if (p >= 0) n = n.substring(0, p) + n.substr(p + 1, 2);
unit = unit.substr(unit.length - n.length);
for (var i = 0; i < n.length; i++) str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
str = str.replace(/零(仟
[解决办法]
佰
[解决办法]
拾
[解决办法]
角)/g, "零").replace(/(零)+/g, "零").replace(/零(万
[解决办法]
亿)/g, "$1").replace(/(亿)万
[解决办法]
壹(拾)/g, "$1$2").replace(/^点零?
[解决办法]
零分/g, "").replace(/点$/g, "").replace(/角
[解决办法]
分/g, "") + "千克";
if(str.indexOf("负零点")==0
[解决办法]
str.indexOf("零点")==0){}else{str=str.replace("零点","点")}
if (str == "千克") return "零千克";
else return str;
}
document.write(ToDX(-0.870)+"<br>");
document.write(ToDX(0.870)+"<br>");
document.write(ToDX(7890.870)+"<br>");
document.write(ToDX(-7800.870)+"<br>");