修复浮点数计算精度问题
This commit is contained in:
parent
2acaed7a09
commit
ab7c159b01
@ -88,6 +88,38 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, computed } from 'vue'
|
import { ref, computed } from 'vue'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
|
// 精确浮点数加法函数
|
||||||
|
const floatAdd = (a, b) => {
|
||||||
|
var c, d, e;
|
||||||
|
if (undefined === a || null === a || "" === a || isNaN(a)) { a = 0; }
|
||||||
|
if (undefined === b || null === b || "" === b || isNaN(b)) { b = 0; }
|
||||||
|
try {
|
||||||
|
c = a.toString().split(".")[1].length;
|
||||||
|
} catch (f) {
|
||||||
|
c = 0;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
d = b.toString().split(".")[1].length;
|
||||||
|
} catch (f) {
|
||||||
|
d = 0;
|
||||||
|
}
|
||||||
|
e = Math.pow(10, Math.max(c, d));
|
||||||
|
return (floatMul(a, e) + floatMul(b, e)) / e;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 精确浮点数乘法函数
|
||||||
|
const floatMul = (a, b) => {
|
||||||
|
var c = 0,
|
||||||
|
d = a.toString(),
|
||||||
|
e = b.toString();
|
||||||
|
try {
|
||||||
|
c += d.split(".")[1].length;
|
||||||
|
} catch (f) {}
|
||||||
|
try {
|
||||||
|
c += e.split(".")[1].length;
|
||||||
|
} catch (f) {}
|
||||||
|
return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
|
||||||
|
}
|
||||||
|
|
||||||
const dialogVisible = ref(false)
|
const dialogVisible = ref(false)
|
||||||
const rows = ref<Record<string, any>[]>([])
|
const rows = ref<Record<string, any>[]>([])
|
||||||
@ -95,12 +127,19 @@ const rows = ref<Record<string, any>[]>([])
|
|||||||
const nowDate = dayjs().format('YYYY-MM-DD')
|
const nowDate = dayjs().format('YYYY-MM-DD')
|
||||||
const duEmpName = computed(() => (rows.value[0] ? rows.value[0]['合立经手人'] ?? '' : ''))
|
const duEmpName = computed(() => (rows.value[0] ? rows.value[0]['合立经手人'] ?? '' : ''))
|
||||||
|
|
||||||
const sumQuantity = computed(() =>
|
// 计算总数量
|
||||||
rows.value.reduce((acc, cur) => acc + Number(cur['数量'] || 0), 0)
|
const sumQuantity = computed(() => {
|
||||||
)
|
return rows.value.reduce((acc, cur) => {
|
||||||
const sumTotal = computed(() =>
|
return floatAdd(acc, Number(cur['数量']) || 0)
|
||||||
rows.value.reduce((acc, cur) => acc + Number(cur['总价格'] || 0), 0)
|
}, 0)
|
||||||
)
|
})
|
||||||
|
|
||||||
|
// 计算总价格
|
||||||
|
const sumTotal = computed(() => {
|
||||||
|
return rows.value.reduce((acc, cur) => {
|
||||||
|
return floatAdd(acc, Number(cur['总价格']) || 0)
|
||||||
|
}, 0)
|
||||||
|
})
|
||||||
|
|
||||||
const fmtDate = (v: any) => (v ? dayjs(v).format('YYYY-MM-DD') : '')
|
const fmtDate = (v: any) => (v ? dayjs(v).format('YYYY-MM-DD') : '')
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user