1、数字转人民币(小写)
/**
* 把数字转换成RMB格式
* @param v
* @returns {String}
*/
function RMBMoney(v){
v = (Math.round((v-0)*100))/100;
v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
v = String(v);
var ps = v.split('.');
var whole = ps[0];
var sub = ps[1] ? '.'+ ps[1] : '.00';
var r = /(\d+)(\d{3})/;
while (r.test(whole)) {
whole = whole.replace(r, '$1' + ',' + '$2');
}
v = whole + sub;
if(v.charAt(0) == '-'){
return'-¥' + v.substr(1);
}
return"¥" + v;
}
2、数字转大写人民币
/**
* 数字转大写RMB(以下4个函数)
* @param n
* @returns
*/
function i2c(n) {
if (typeof (n) == "number") {
if (-1 == n.toString().indexOf(".")) {
return seti2c(n);
} else {
var i, istr, f, fstr, a, rstr;
a = n.toString().split(".");
i = a[0];
f = a[1];
istr = seti2c(i);
fstr = setf2c(f);
rstr = istr + fstr;
rstr = rstr.replace(/^元/, "");
return rstr;
}
} else {
return"---";
}
}
function seti2c(n) {
var ns = n.toString();
var tempstr = "";
for ( var i = 1; i < ns.length + 1; i++) {
switch (i) {
case 1:
var t = ns.substr(ns.length - i, 1);
if (t != "0") {
tempstr = t + "元" + tempstr;
} else {
tempstr = "元" + tempstr;
}
break;
case 2:
var t = ns.substr(ns.length - i, 1);
if (t != "0") {
tempstr = t + "拾" + tempstr;
} else {
tempstr = "0" + tempstr;
}
break;
case 3:
var t = ns.substr(ns.length - i, 1);
if (t != "0") {
tempstr = t + "佰" + tempstr;
} else {
tempstr = "0" + tempstr;
}
break;
case 4:
var t = ns.substr(ns.length - i, 1);
if (t != "0") {
tempstr = t + "仟" + tempstr;
} else {
tempstr = "0" + tempstr;
}
break;
case 5:
var t = ns.substr(ns.length - i, 1);
if (t != "0") {
tempstr = t + "万" + tempstr;
} else {
tempstr = "万" + tempstr;
}
break;
case 6:
var t = ns.substr(ns.length - i, 1);
if (t != "0") {
tempstr = t + "拾" + tempstr;
} else {
tempstr = "0" + tempstr;
}
break;
case 7:
var t = ns.substr(ns.length - i, 1);
if (t != "0") {
tempstr = t + "佰" + tempstr;
} else {
tempstr = "0" + tempstr;
}
break;
case 8:
var t = ns.substr(ns.length - i, 1);
if (t != "0") {
tempstr = t + "仟" + tempstr;
} else {
tempstr = "0" + tempstr;
}
break;
case 9:
var t = ns.substr(ns.length - i, 1);
if (t != "0") {
tempstr = t + "亿" + tempstr;
} else {
tempstr = "亿" + tempstr;
}
break;
case 10:
var t = ns.substr(ns.length - i, 1);
if (t != "0") {
tempstr = t + "拾" + tempstr;
} else {
tempstr = "0" + tempstr;
}
break;
case 11:
var t = ns.substr(ns.length - i, 1);
if (t != "0") {
tempstr = t + "佰" + tempstr;
} else {
tempstr = "0" + tempstr;
}
break;
case 12:
var t = ns.substr(ns.length - i, 1);
if (t != "0") {
tempstr = t + "仟" + tempstr;
} else {
tempstr = "0" + tempstr;
}
break;
default:
break;
}
}
return g2b(tempstr);
}
function setf2c(n) {
var ns = n.toString();
var tempstr = "";
for ( var i = 0; i < ns.length; i++) {
switch (i) {
case 0:
var t = ns.substr(i, 1);
if (t != "0") {
tempstr += t + "角";
} else {
tempstr += "0";
}
break;
case 1:
var t = ns.substr(i, 1);
if (t != "0") {
tempstr += t + "分";
}
break;
default:
break;
}
}
return g2b(tempstr);
}
function g2b(s) {
rs = s.replace(/0+/g, "0");
rs = rs.replace(/0(元|万|亿)/g, "$1");
rs = rs.replace("亿万", "亿");
rs = rs.replace(/0/g, "零");
rs = rs.replace(/1/g, "壹");
rs = rs.replace(/2/g, "贰");
rs = rs.replace(/3/g, "叁");
rs = rs.replace(/4/g, "肆");
rs = rs.replace(/5/g, "伍");
rs = rs.replace(/6/g, "陆");
rs = rs.replace(/7/g, "柒");
rs = rs.replace(/8/g, "捌");
rs = rs.replace(/9/g, "玖");
return rs;
}
3、汉字转拼音(简拼+全拼)
下载地址:http://img1.51cto.com/attachment/201204/67690_1334811843.zip
/**
* @基本描述 汉字转拼音--全拼
* @参数列表 @param l1
* @参数列表 @returns {String}
* @return String
* @throws
* @since 1.0.0
*/
function CC2QP(l1);
/**
* @基本描述 汉字转拼音--简拼
* @参数列表 @param l1
* @参数列表 @returns {String}
* @return String
* @throws
* @since 1.0.0
*/
function CC2JP(l1);
4、IE6下字符串转成日期类型出现NaN问题
function parseISO8601(dateStringInRange) { var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/, date = new Date(NaN), month, parts = isoExp.exec(dateStringInRange); if(parts) { month = +parts[2]; date.setFullYear(parts[1], month - 1, parts[3]); if(month != date.getMonth() + 1) { date.setTime(NaN); } } return date; }
5、金额格式化为XXX,XXX,XXX.XX
function formatNum(num,n) { num = String(num.toFixed(n)); var re = /(-?\d+)(\d{3})/; while(re.test(num)) num = num.replace(re,"$1,$2") return num; } document.write(formatNum(1234005651.789,2));