From c8eb2e42ac910764d1e2eec9d561c48dff79cae7 Mon Sep 17 00:00:00 2001 From: liihuu Date: Tue, 30 Apr 2024 00:32:39 +0800 Subject: [PATCH] fix: fix decimal fold error. #553 --- src/common/utils/format.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/common/utils/format.ts b/src/common/utils/format.ts index 10b99bda5..6e079fb77 100644 --- a/src/common/utils/format.ts +++ b/src/common/utils/format.ts @@ -119,11 +119,15 @@ export function formatThousands (value: string | number, sign: string): string { export function formatFoldDecimal (value: string | number, threshold: number): string { const vl = `${value}` - const match = vl.match(/\.0*(\d+)/) - if (isValid(match) && parseInt(match[1]) > 0) { - const count = match[0].length - 1 - match[1].length - if (count >= threshold) { - return vl.replace(/\.0*/, `.0{${count}}`) + const reg = new RegExp('.0{' + threshold + ',}[1-9]\\d*$') + if (reg.test(vl)) { + const result = vl.split('.') + const v = result[result.length - 1] + const match = v.match(/0*/) + if (isValid(match)) { + const count = match[0].length + result[result.length - 1] = v.replace(/0*/, `0{${count}}`) + return result.join('.') } } return vl