fix: 修复积分显示的浮点数精度问题

使用 Math.round(score * 100) / 100 避免 JavaScript 浮点数计算误差
例如 0.1 × 46 现在正确显示为 4.60 而不是 4.600000000000005
This commit is contained in:
Your Name 2026-01-15 15:47:59 +08:00
parent 3922d3653b
commit eeeba7b685
13 changed files with 39 additions and 26 deletions

View File

@ -162,10 +162,11 @@ const filteredAchievements = computed(() => {
return result
})
// 格式化积分(保留两位小数)
// 格式化积分(保留两位小数,修复浮点数精度问题)
const formatScore = (score: number | null | undefined): string => {
if (score === null || score === undefined) return '0.00'
return Number(score).toFixed(2)
// 先乘以100,四舍五入取整,再除以100,避免浮点数精度问题
return (Math.round(Number(score) * 100) / 100).toFixed(2)
}
// 加载所有成就

View File

@ -1148,10 +1148,11 @@ const formatDateOnly = (date: string | undefined) => {
return dayjs(date).format('YYYY-MM-DD')
}
// 格式化积分(保留两位小数)
// 格式化积分(保留两位小数,修复浮点数精度问题)
const formatScore = (score: number | null | undefined): string => {
if (score === null || score === undefined) return '0.00'
return Number(score).toFixed(2)
// 先乘以100,四舍五入取整,再除以100,避免浮点数精度问题
return (Math.round(Number(score) * 100) / 100).toFixed(2)
}
// 单独加载设计师绩效数据(用于部门筛选)

View File

@ -264,10 +264,11 @@ const calculateReward = (continuousDays: number): number => {
}
}
// 格式化积分(保留两位小数)
// 格式化积分(保留两位小数,修复浮点数精度问题)
const formatScore = (score: number | null | undefined): string => {
if (score === null || score === undefined) return '0.00'
return Number(score).toFixed(2)
// 先乘以100,四舍五入取整,再除以100,避免浮点数精度问题
return (Math.round(Number(score) * 100) / 100).toFixed(2)
}
// 格式化日期

View File

@ -290,10 +290,11 @@ const contestForm = ref({
endTime: null as Dayjs | null
})
// 格式化积分(保留两位小数)
// 格式化积分(保留两位小数,修复浮点数精度问题)
const formatScore = (score: number | null | undefined): string => {
if (score === null || score === undefined) return '0.00'
return Number(score).toFixed(2)
// 先乘以100,四舍五入取整,再除以100,避免浮点数精度问题
return (Math.round(Number(score) * 100) / 100).toFixed(2)
}
const loadData = async () => {

View File

@ -971,10 +971,11 @@ watch(
}
)
// 格式化积分(保留两位小数)
// 格式化积分(保留两位小数,修复浮点数精度问题)
const formatScore = (score: number | null | undefined): string => {
if (score === null || score === undefined) return '0.00'
return Number(score).toFixed(2)
// 先乘以100,四舍五入取整,再除以100,避免浮点数精度问题
return (Math.round(Number(score) * 100) / 100).toFixed(2)
}
// 计算预估积分(累加所有任务类型)

View File

@ -356,10 +356,11 @@ const formatDate = (date: string | null | undefined) => {
return dayjs(date).format('MM/DD HH:mm')
}
// 格式化积分(保留两位小数)
// 格式化积分(保留两位小数,修复浮点数精度问题)
const formatScore = (score: number | null | undefined): string => {
if (score === null || score === undefined) return '0.00'
return Number(score).toFixed(2)
// 先乘以100,四舍五入取整,再除以100,避免浮点数精度问题
return (Math.round(Number(score) * 100) / 100).toFixed(2)
}
// 获取进度条颜色

View File

@ -262,10 +262,11 @@ const goalForm = ref({
rewardScore: 50
})
// 格式化积分(保留两位小数)
// 格式化积分(保留两位小数,修复浮点数精度问题)
const formatScore = (score: number | null | undefined): string => {
if (score === null || score === undefined) return '0.00'
return Number(score).toFixed(2)
// 先乘以100,四舍五入取整,再除以100,避免浮点数精度问题
return (Math.round(Number(score) * 100) / 100).toFixed(2)
}
const loadData = async () => {

View File

@ -252,10 +252,11 @@ import {
const router = useRouter()
const authStore = useAuthStore()
// 格式化积分(保留两位小数)
// 格式化积分(保留两位小数,修复浮点数精度问题)
const formatScore = (score: number | null | undefined): string => {
if (score === null || score === undefined) return '0.00'
return Number(score).toFixed(2)
// 先乘以100,四舍五入取整,再除以100,避免浮点数精度问题
return (Math.round(Number(score) * 100) / 100).toFixed(2)
}
// 用户数据

View File

@ -257,10 +257,11 @@ const formatTime = (time: string | undefined) => {
return dayjs(time).format('YYYY-MM-DD HH:mm')
}
// 格式化积分(保留两位小数)
// 格式化积分(保留两位小数,修复浮点数精度问题)
const formatScore = (score: number | null | undefined): string => {
if (score === null || score === undefined) return '0.00'
return Number(score).toFixed(2)
// 先乘以100,四舍五入取整,再除以100,避免浮点数精度问题
return (Math.round(Number(score) * 100) / 100).toFixed(2)
}
// 判断是否逾期

View File

@ -581,10 +581,11 @@ const formatTime = (time: string | undefined) => {
return dayjs(time).format('YYYY-MM-DD HH:mm')
}
// 格式化积分(保留两位小数)
// 格式化积分(保留两位小数,修复浮点数精度问题)
const formatScore = (score: number | null | undefined): string => {
if (score === null || score === undefined) return '0.00'
return Number(score).toFixed(2)
// 先乘以100,四舍五入取整,再除以100,避免浮点数精度问题
return (Math.round(Number(score) * 100) / 100).toFixed(2)
}
// 判断是否逾期

View File

@ -101,10 +101,11 @@ const totalScore = computed(() => {
return formatScore(total)
})
// 格式化积分(保留两位小数)
// 格式化积分(保留两位小数,修复浮点数精度问题)
const formatScore = (score: number | null | undefined): string => {
if (score === null || score === undefined) return '0.00'
return Number(score).toFixed(2)
// 先乘以100,四舍五入取整,再除以100,避免浮点数精度问题
return (Math.round(Number(score) * 100) / 100).toFixed(2)
}
const totalCount = computed(() => total.value)

View File

@ -2891,10 +2891,11 @@ const formatDate = (date: string | undefined) => {
return dayjs(date).format('YYYY-MM-DD')
}
// 格式化积分(保留两位小数)
// 格式化积分(保留两位小数,修复浮点数精度问题)
const formatScore = (score: number | null | undefined): string => {
if (score === null || score === undefined) return '0.00'
return Number(score).toFixed(2)
// 先乘以100,四舍五入取整,再除以100,避免浮点数精度问题
return (Math.round(Number(score) * 100) / 100).toFixed(2)
}
// 格式化截止时间

View File

@ -419,10 +419,11 @@ const recordPagination = ref({
total: 0
})
// 格式化积分(保留两位小数)
// 格式化积分(保留两位小数,修复浮点数精度问题)
const formatScore = (score: number | null | undefined): string => {
if (score === null || score === undefined) return '0.00'
return Number(score).toFixed(2)
// 先乘以100,四舍五入取整,再除以100,避免浮点数精度问题
return (Math.round(Number(score) * 100) / 100).toFixed(2)
}
const loadData = async () => {