From a45d8d4149ccdc1a628723a17aa5cb50da7e613b Mon Sep 17 00:00:00 2001 From: yuepenglong <1547476325@qq.com> Date: Fri, 11 Apr 2025 17:44:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/electionList/info.vue | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/pages/electionList/info.vue b/src/pages/electionList/info.vue index b0bf738..bb9fa31 100644 --- a/src/pages/electionList/info.vue +++ b/src/pages/electionList/info.vue @@ -3,6 +3,20 @@ import { getVoteDetail } from '@/api/common' const info = ref({}) +function toFixed2(value: any, defaultValue = '0.00') { + // 容错处理:空值、非数字字符串、布尔值等 + const number = Number(value); + if (isNaN(number)) { + return defaultValue; + } + + // 解决 1.005.toFixed(2) => 1.00 的问题 + const fixedNumber = Math.round(number * 100) / 100; + + // 处理 toFixed(2) 可能仍存在的精度问题(如 1.0005 经过处理后为 1.0) + return fixedNumber.toFixed(2); +} + onLoad((opt) => { getVoteDetail({ id: opt?.id }).then((res: any) => { info.value = res.data @@ -46,21 +60,21 @@ onLoad((opt) => { 同意 - {{ v.agree_num }} 票({{ v.ageree_percent.toFixed(2) }}%) + {{ v.agree_num }} 票({{ toFixed2(v.ageree_percent) }}%) 反对 - {{ v.disagree_num }} 票({{ v.giveup_percent.toFixed(2) }}%) + {{ v.disagree_num }} 票({{ toFixed2(v.giveup_percent) }}%) 弃权 - {{ v.giveup_num }} 票({{ v.giveup_percent.toFixed(2) }}%) + {{ v.giveup_num }} 票({{ toFixed2(v.giveup_percent) }}%)