数学计算添加进制转换

This commit is contained in:
fofolee 2025-01-10 19:35:02 +08:00
parent 5e70c4c9a0
commit dc7a2c9049
5 changed files with 70 additions and 204 deletions

View File

@ -1,198 +1,40 @@
/**
* 长度单位转换
* @param {number} value
* @param {string} fromUnit 源单位
* @param {string} toUnit 目标单位
* @returns {number} 转换结果
* 进制转换
* @param {string|number} value 要转换的
* @param {string} fromBase 源进制
* @param {string} toBase 目标进制
* @returns {string} 转换结果
*/
function length(value, fromUnit, toUnit) {
const units = {
mm: 0.001, // 毫米
cm: 0.01, // 厘米
dm: 0.1, // 分米
m: 1, // 米(基准单位)
km: 1000, // 千米
in: 0.0254, // 英寸
ft: 0.3048, // 英尺
yd: 0.9144, // 码
mi: 1609.344, // 英里
function base(value, fromBase, toBase) {
const bases = {
binary: 2,
octal: 8,
decimal: 10,
hex: 16,
};
if (!(fromUnit in units)) {
throw new Error(`不支持的源单位: ${fromUnit}`);
if (!(fromBase in bases)) {
throw new Error(`不支持的源进制: ${fromBase}`);
}
if (!(toUnit in units)) {
throw new Error(`不支持的目标单位: ${toUnit}`);
if (!(toBase in bases)) {
throw new Error(`不支持的目标进制: ${toBase}`);
}
// 先转换为米,再转换为目标单位
return (value * units[fromUnit]) / units[toUnit];
// 先转为十进制
let decimal;
if (fromBase === "decimal") {
decimal = Number(value);
} else {
decimal = parseInt(String(value), bases[fromBase]);
}
/**
* 重量单位转换
* @param {number} value 数值
* @param {string} fromUnit 源单位
* @param {string} toUnit 目标单位
* @returns {number} 转换结果
*/
function weight(value, fromUnit, toUnit) {
const units = {
mg: 0.001, // 毫克
g: 1, // 克(基准单位)
kg: 1000, // 千克
t: 1000000, // 吨
oz: 28.3495, // 盎司
lb: 453.592, // 磅
};
if (!(fromUnit in units)) {
throw new Error(`不支持的源单位: ${fromUnit}`);
// 再转为目标进制
if (toBase === "decimal") {
return String(decimal);
}
if (!(toUnit in units)) {
throw new Error(`不支持的目标单位: ${toUnit}`);
}
// 先转换为克,再转换为目标单位
return (value * units[fromUnit]) / units[toUnit];
}
/**
* 面积单位转换
* @param {number} value 数值
* @param {string} fromUnit 源单位
* @param {string} toUnit 目标单位
* @returns {number} 转换结果
*/
function area(value, fromUnit, toUnit) {
const units = {
mm2: 0.000001, // 平方毫米
cm2: 0.0001, // 平方厘米
dm2: 0.01, // 平方分米
m2: 1, // 平方米(基准单位)
km2: 1000000, // 平方千米
ha: 10000, // 公顷
in2: 0.00064516, // 平方英寸
ft2: 0.092903, // 平方英尺
yd2: 0.836127, // 平方码
ac: 4046.86, // 英亩
mi2: 2589988.11, // 平方英里
};
if (!(fromUnit in units)) {
throw new Error(`不支持的源单位: ${fromUnit}`);
}
if (!(toUnit in units)) {
throw new Error(`不支持的目标单位: ${toUnit}`);
}
// 先转换为平方米,再转换为目标单位
return (value * units[fromUnit]) / units[toUnit];
}
/**
* 体积单位转换
* @param {number} value 数值
* @param {string} fromUnit 源单位
* @param {string} toUnit 目标单位
* @returns {number} 转换结果
*/
function volume(value, fromUnit, toUnit) {
const units = {
ml: 0.001, // 毫升
l: 1, // 升(基准单位)
m3: 1000, // 立方米
cm3: 0.001, // 立方厘米
mm3: 0.000001, // 立方毫米
gal: 3.78541, // 加仑(美制)
qt: 0.946353, // 夸脱(美制)
pt: 0.473176, // 品脱(美制)
cup: 0.236588, // 杯(美制)
floz: 0.0295735, // 液量盎司(美制)
};
if (!(fromUnit in units)) {
throw new Error(`不支持的源单位: ${fromUnit}`);
}
if (!(toUnit in units)) {
throw new Error(`不支持的目标单位: ${toUnit}`);
}
// 先转换为升,再转换为目标单位
return (value * units[fromUnit]) / units[toUnit];
}
/**
* 温度单位转换
* @param {number} value 数值
* @param {string} fromUnit 源单位
* @param {string} toUnit 目标单位
* @returns {number} 转换结果
*/
function temperature(value, fromUnit, toUnit) {
const conversions = {
C: {
F: (v) => (v * 9) / 5 + 32,
K: (v) => v + 273.15,
C: (v) => v,
},
F: {
C: (v) => ((v - 32) * 5) / 9,
K: (v) => ((v - 32) * 5) / 9 + 273.15,
F: (v) => v,
},
K: {
C: (v) => v - 273.15,
F: (v) => ((v - 273.15) * 9) / 5 + 32,
K: (v) => v,
},
};
if (!(fromUnit in conversions)) {
throw new Error(`不支持的源单位: ${fromUnit}`);
}
if (!(toUnit in conversions[fromUnit])) {
throw new Error(`不支持的目标单位: ${toUnit}`);
}
return conversions[fromUnit][toUnit](value);
}
/**
* 时间单位转换
* @param {number} value 数值
* @param {string} fromUnit 源单位
* @param {string} toUnit 目标单位
* @returns {number} 转换结果
*/
function time(value, fromUnit, toUnit) {
const units = {
ms: 0.001, // 毫秒
s: 1, // 秒(基准单位)
min: 60, // 分钟
h: 3600, // 小时
d: 86400, // 天
w: 604800, // 周
mo: 2592000, // 月按30天计算
y: 31536000, // 年按365天计算
};
if (!(fromUnit in units)) {
throw new Error(`不支持的源单位: ${fromUnit}`);
}
if (!(toUnit in units)) {
throw new Error(`不支持的目标单位: ${toUnit}`);
}
// 先转换为秒,再转换为目标单位
return (value * units[fromUnit]) / units[toUnit];
return decimal.toString(bases[toBase]);
}
module.exports = {
length,
weight,
area,
volume,
temperature,
time,
base,
};

View File

@ -10,6 +10,6 @@ module.exports = {
statistics,
geometry,
trigonometry,
conversion,
random,
conversion,
};

View File

@ -148,6 +148,8 @@ export default defineComponent({
}
},
runCommand() {
if (!this.localCommand.code)
return quickcommand.showMessageBox("请检查参数是否正确", "info");
//
const tempCommand = {
...this.localCommand,

View File

@ -1,4 +1,10 @@
<template>
<component
:is="!!label ? 'BorderLabel' : 'div'"
:label="label"
:icon="icon"
:model-value="isCollapse"
>
<div class="button-group">
<div
v-for="opt in options"
@ -9,13 +15,18 @@
{{ opt.label }}
</div>
</div>
</component>
</template>
<script>
import { defineComponent } from "vue";
import BorderLabel from "./BorderLabel.vue";
export default defineComponent({
name: "ButtonGroup",
components: {
BorderLabel,
},
props: {
modelValue: {
required: true,
@ -24,6 +35,18 @@ export default defineComponent({
type: Array,
required: true,
},
label: {
type: String,
default: "",
},
icon: {
type: String,
default: "",
},
isCollapse: {
type: Boolean,
default: false,
},
},
emits: ["update:modelValue"],
});

View File

@ -314,22 +314,23 @@ export const mathCommands = {
],
},
{
value: "quickcomposer.math.conversion.decimal",
label: "单位换算",
desc: "数学单位换算",
value: "quickcomposer.math.conversion.base",
label: "进制转换",
desc: "数字进制转换",
icon: "swap_horiz",
config: [
{
key: "value",
label: "数值",
component: "NumberInput",
component: "VariableInput",
icon: "pin",
width: 12,
defaultValue: newVarInputVal("var", ""),
},
{
key: "from",
label: "从",
component: "q-select",
component: "ButtonGroup",
icon: "input",
width: 6,
options: [
@ -337,14 +338,13 @@ export const mathCommands = {
{ label: "二进制", value: "binary" },
{ label: "八进制", value: "octal" },
{ label: "十六进制", value: "hex" },
{ label: "度", value: "degrees" },
{ label: "弧度", value: "radians" },
],
defaultValue: "decimal",
},
{
key: "to",
label: "到",
component: "q-select",
component: "ButtonGroup",
icon: "output",
width: 6,
options: [
@ -352,9 +352,8 @@ export const mathCommands = {
{ label: "二进制", value: "binary" },
{ label: "八进制", value: "octal" },
{ label: "十六进制", value: "hex" },
{ label: "度", value: "degrees" },
{ label: "弧度", value: "radians" },
],
defaultValue: "hex",
},
],
},