以下是一个使用jQuery将数字金额转换为大写金额的函数示例:
function convertToChineseCapital(money) {
// 定义数字和单位数组
var cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
var cnIntRadice = ['', '拾', '佰', '仟'];
var cnIntUnits = ['', '拾', '佰', '仟', '万', '亿', '兆'];
var cnDecUnits = ['角', '分', '厘', '厘'];
var cnInteger = '整';
var cnIntLast = '元';
var maxNum = 999999999999999.9999;
var integerNum;
var decimalNum;
var chineseStr = '';
var parts;
if (money == '') {
return '';
}
money = parseFloat(money);
if (money >= maxNum) {
return '';
}
if (money < 0) {
return '';
}
money = money.toString();
if (money.indexOf('.') == -1) {
integerNum = money;
decimalNum = '';
} else {
parts = money.split('.');
integerNum = parts[0];
decimalNum = parts[1].substr(0, 4);
}
if (parseInt(integerNum, 10) > 0) {
var zeroCount = 0;
for (var i = 0; i < integerNum.length; i++) {
if (integerNum.charAt(i) == '0') {
zeroCount++;
} else {
if (zeroCount > 0) {
chineseStr += cnNums[0];
}
zeroCount = 0;
chineseStr += cnNums[parseInt(integerNum.charAt(i), 10)] + cnIntRadice[integerNum.length - i - 1];
}
chineseStr += cnIntUnits[integerNum.length - i - 1];
}
chineseStr += cnIntLast;
}
if (decimalNum != '') {
for (var i = 0; i < decimalNum.length; i++) {
chineseStr += cnNums[parseInt(decimalNum.charAt(i), 10)] + cnDecUnits[i];
}
}
if (chineseStr == '') {
chineseStr += cnNums[0] + cnIntLast;
} else if (decimalNum == '') {
chineseStr += cnInteger;
}
return chineseStr;
}
$(document).ready(function(){
// 假设您有一个输入框和一个显示转换结果的元素
$('#convert').click(function(){
var num = $('#number').val(); // 获取输入的数字
var chineseNum = convertToChineseCapital(num); // 转换为大写金额
$('#result').text(chineseNum); // 显示转换结果
});
});
在HTML中,您可以这样使用:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数字转大写金额