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) }}%)