HTML版

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>小写转大写金额</title>
<style>
body {
font-family: Arial, sans-serif
display: flex
justify-content: center
align-items: center
height: 100vh
margin: 0
background-color:
}
.container {
background-color:
padding: 20px
border-radius: 8px
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1)
text-align: center
}
input,
button {
margin: 10px
padding: 10px
border: 1px solid
border-radius: 4px
}
button {
background-color:
color: white
cursor: pointer
}
button:hover {
background-color:
}
</style>
</head>
<body>
<div class="container">
<h2>小写转大写金额</h2>
<input type="number" id="amount" placeholder="请输入金额" step="0.01">
<button onclick="convertAmount()">转换</button>
<p id="result"></p>
</div>
<script>
const CN_NUMBERS = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]
const CN_UNITS = ["", "拾", "佰", "仟"]
const CN_BIG_UNITS = ["", "万", "亿"]
const CN_DECIMAL_UNITS = ["角", "分"]
function convert(amount) {
if (amount < 0 || amount > 999999999999.99) {
return "金额超出范围(0-999999999999.99)"
}
let integerPart = Math.floor(amount)
let decimalPart = Math.round((amount - integerPart) * 100)
let integerStr = convertIntegerPart(integerPart)
let decimalStr = convertDecimalPart(decimalPart)
// 处理整数部分为零的情况
if (integerPart === 0) {
return decimalStr === "整" ? "零元整" : decimalStr
} else {
return integerStr + "元" + decimalStr
}
}
function convertIntegerPart(number) {
if (number === 0) {
return CN_NUMBERS[0]
}
let strNumber = String(number)
let result = ""
let lastZero = false
let hasUnit = false
for (let i = 0
let c = strNumber.charAt(i)
let digit = parseInt(c)
let pos = strNumber.length - i - 1
if (digit !== 0) {
if (lastZero) {
result += CN_NUMBERS[0]
lastZero = false
}
result += CN_NUMBERS[digit]
if (pos % 4 !== 0) {
result += CN_UNITS[pos % 4]
}
hasUnit = true
} else {
lastZero = true
}
// 处理万、亿单位
if (pos % 4 === 0 && pos !== 0) {
if (hasUnit) {
result += CN_BIG_UNITS[Math.floor(pos / 4)]
hasUnit = false
}
lastZero = false
}
}
return result
}
function convertDecimalPart(decimal) {
if (decimal === 0) {
return "整"
}
let result = ""
let jiao = Math.floor(decimal / 10)
let fen = decimal % 10
if (jiao === 0 && fen !== 0) {
result += CN_NUMBERS[0]
}
if (jiao > 0) {
result += CN_NUMBERS[jiao] + CN_DECIMAL_UNITS[0]
}
if (fen > 0) {
result += CN_NUMBERS[fen] + CN_DECIMAL_UNITS[1]
}
return result
}
function convertAmount() {
let amount = parseFloat(document.getElementById('amount').value)
let result = convert(amount)
document.getElementById('result').textContent = "大写金额: " + result
}
</script>
</body>
</html>
Java版
public class AmountConverter {
private static final String[] CN_NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}
private static final String[] CN_UNITS = {"", "拾", "佰", "仟"}
private static final String[] CN_BIG_UNITS = {"", "万", "亿"}
private static final String[] CN_DECIMAL_UNITS = {"角", "分"}
public static String converter(double amount) {
if (amount < 0 || amount > 999999999999.99) {
return "金额超出范围(0-999999999999.99)"
}
long integerPart = (long) amount
int decimalPart = (int) Math.round((amount - integerPart) * 100)
String integerStr = convertIntegerPart(integerPart)
String decimalStr = convertDecimalPart(decimalPart)
// 处理整数部分为零的情况
if (integerPart == 0) {
return decimalStr.equals("整") ? "零元整" : decimalStr
} else {
return integerStr + "元" + decimalStr
}
}
private static String convertIntegerPart(long number) {
if (number == 0) {
return CN_NUMBERS[0]
}
String strNumber = String.valueOf(number)
StringBuilder result = new StringBuilder()
boolean lastZero = false
boolean hasUnit = false
for (int i = 0
char c = strNumber.charAt(i)
int digit = c - '0'
int pos = strNumber.length() - i - 1
if (digit != 0) {
if (lastZero) {
result.append(CN_NUMBERS[0])
lastZero = false
}
result.append(CN_NUMBERS[digit])
if (pos % 4 != 0) {
result.append(CN_UNITS[pos % 4])
}
hasUnit = true
} else {
lastZero = true
}
// 处理万、亿单位
if (pos % 4 == 0 && pos != 0) {
if (hasUnit) {
result.append(CN_BIG_UNITS[pos / 4])
hasUnit = false
}
lastZero = false
}
}
return result.toString()
}
private static String convertDecimalPart(int decimal) {
if (decimal == 0) {
return "整"
}
StringBuilder result = new StringBuilder()
int jiao = decimal / 10
int fen = decimal % 10
if (jiao == 0 && fen != 0) {
result.append(CN_NUMBERS[0])
}
if (jiao > 0) {
result.append(CN_NUMBERS[jiao]).append(CN_DECIMAL_UNITS[0])
}
if (fen > 0) {
result.append(CN_NUMBERS[fen]).append(CN_DECIMAL_UNITS[1])
}
return result.toString()
}
public static void main(String[] args) {
System.out.println(converter(1000100.05))
System.out.println(converter(1234567.89))
System.out.println(converter(0.56))
System.out.println(converter(10000001))
System.out.println(converter(0))
System.out.println(converter(1))
System.out.println(converter(10))
System.out.println(converter(100))
System.out.println(converter(1000))
System.out.println(converter(10000))
System.out.println(converter(100000))
System.out.println(converter(1000000))
System.out.println(converter(10000000))
System.out.println(converter(100000000))
System.out.println(converter(999999999))
System.out.println(converter(999999999.99))
System.out.println(converter(999999999999.99))
System.out.println(converter(9876543210.99))
System.out.println(converter(9876543210d))
}
}