51 lines
1.6 KiB
HTML
51 lines
1.6 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>电容值转换</title>
|
|
</head>
|
|
<body>
|
|
<h1>电容值转换工具</h1>
|
|
<label for="capInput">输入电容值:</label>
|
|
<input type="text" id="capInput" placeholder="输入电容值">
|
|
<button onclick="convertCap()">转换</button>
|
|
<p id="output">转换结果将在这里显示</p>
|
|
|
|
<script>
|
|
function cap_namer(value_str) {
|
|
value_str = value_str.toLowerCase();
|
|
if (value_str.includes('u')) {
|
|
base = 1000000;
|
|
} else if (value_str.includes('n')) {
|
|
base = 1000;
|
|
} else if (value_str.includes('p')) {
|
|
base = 1;
|
|
} else {
|
|
throw '错误的值';
|
|
}
|
|
value_flt = parseFloat(value_str);
|
|
inner_flt = value_flt * base;
|
|
if (inner_flt < 10) {
|
|
code = inner_flt.toString().replace('.', 'R');
|
|
} else {
|
|
code = inner_flt.toString().slice(0, 2) + (inner_flt.toString().length - 4);
|
|
}
|
|
return code;
|
|
}
|
|
|
|
function convertCap() {
|
|
const inputElement = document.getElementById("capInput");
|
|
const inputValue = inputElement.value;
|
|
const outputElement = document.getElementById("output");
|
|
|
|
try {
|
|
const convertedValue = cap_namer(inputValue);
|
|
outputElement.textContent = "转换结果:" + convertedValue;
|
|
} catch (error) {
|
|
outputElement.textContent = "错误:" + error;
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|