添加小阻值判断

This commit is contained in:
Qubot 2023-07-04 16:55:10 +08:00
parent 7d9a5d4616
commit 9b80b3e646

71
1.html
View File

@ -1,14 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>电容值转换</title>
<title>电容/电阻值转换</title>
</head>
<body>
<h1>电容值转换工具</h1>
<label for="capInput">输入电容值:</label>
<input type="text" id="capInput" placeholder="输入电容值">
<button onclick="convertCap()">转换</button>
<h1>电容/电阻值转换工具</h1>
<label for="typeSelect">选择类型:</label>
<select id="typeSelect">
<option value="capacitance">电容</option>
<option value="resistance">电阻</option>
</select>
<br><br>
<label for="valueInput">输入值:</label>
<input type="text" id="valueInput" placeholder="输入电容/电阻值" onkeydown="handleKeyDown(event)">
<button onclick="convertValue()">转换</button>
<p id="output">转换结果将在这里显示</p>
<script>
@ -26,25 +34,66 @@
value_flt = parseFloat(value_str);
inner_flt = value_flt * base;
if (inner_flt < 10) {
code = inner_flt.toString().replace('.', 'R');
code = inner_flt.toString().replace('.', '');
} 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;
function res_namer(value_str) {
value_str = value_str.toLowerCase();
if (value_str.includes('k')) {
base = 1000;
} else if (value_str.includes('m')) {
base = 1000000;
} else if (value_str.includes('g')) {
base = 1000000000;
} else if (value_str.includes('r')) {
base = 1;
} else {
throw '错误的值';
}
value_flt = parseFloat(value_str);
inner_flt = value_flt * base;
if (inner_flt < 1 && inner_flt > 0) {
inner_flt=inner_flt*10
code = "0"+"R"+inner_flt.toString().replace('.', '');
}
else if (inner_flt < 10 && inner_flt > 1) {
code = inner_flt.toString().replace('.', '');
} else {
code = inner_flt.toString().slice(0, 2) + (inner_flt.toString().length - 2);
}
return code;
}
function convertValue() {
const typeSelect = document.getElementById("typeSelect");
const valueInput = document.getElementById("valueInput");
const outputElement = document.getElementById("output");
const valueType = typeSelect.value;
const inputValue = valueInput.value;
try {
const convertedValue = cap_namer(inputValue);
let convertedValue;
if (valueType === "capacitance") {
convertedValue = "C" + cap_namer(inputValue);
} else if (valueType === "resistance") {
convertedValue = "R" + res_namer(inputValue);
}
outputElement.textContent = "转换结果:" + convertedValue;
} catch (error) {
outputElement.textContent = "错误:" + error;
}
}
function handleKeyDown(event) {
if (event.key === "Enter") {
convertValue();
}
}
</script>
</body>
</html>
</html>