From cccca16e258ac616c11b7d697b9416b3975aca07 Mon Sep 17 00:00:00 2001 From: allenve Date: Fri, 17 Nov 2023 15:56:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(amis):input-number=20=E5=B0=8F=E6=95=B0?= =?UTF-8?q?=E5=85=89=E6=A0=87=E5=81=8F=E7=A7=BB=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/amis-core/__tests__/utils/math.test.ts | 2 ++ packages/amis-core/src/utils/math.ts | 12 ++++++++---- packages/amis/src/renderers/Form/InputNumber.tsx | 5 +++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/amis-core/__tests__/utils/math.test.ts b/packages/amis-core/__tests__/utils/math.test.ts index 96101a38add..3f2a4c5e2b1 100644 --- a/packages/amis-core/__tests__/utils/math.test.ts +++ b/packages/amis-core/__tests__/utils/math.test.ts @@ -16,6 +16,8 @@ test(`math safeSub:test`, () => { test('numberFormatter:test', () => { expect(numberFormatter(0)).toEqual('0'); + expect(numberFormatter(0.123)).toEqual('0.123'); + expect(numberFormatter(0.123, 0)).toEqual('0'); expect(numberFormatter(0, 2)).toEqual('0.00'); expect(numberFormatter(0, 8)).toEqual('0.00000000'); expect(numberFormatter(123456)).toEqual('123,456'); diff --git a/packages/amis-core/src/utils/math.ts b/packages/amis-core/src/utils/math.ts index 5a8baa5407e..4aa20edbacb 100644 --- a/packages/amis-core/src/utils/math.ts +++ b/packages/amis-core/src/utils/math.ts @@ -31,14 +31,18 @@ export function safeSub(arg1: number, arg2: number) { return (arg1 * maxDigits - arg2 * maxDigits) / maxDigits; } -export function numberFormatter(num: number | string, precision: number = 0) { +export function numberFormatter(num: number | string, precision?: number) { const ZERO = 0; const number = +num; + const finalP = + typeof precision === 'number' + ? precision + : number.toString().split('.')[1]?.length || 0; if (typeof number === 'number' && !isNaN(number)) { - const regexp = precision ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(\d{3})+$)/g; - return number.toFixed(precision).replace(regexp, '$1,'); + const regexp = finalP ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(\d{3})+$)/g; + return number.toFixed(finalP).replace(regexp, '$1,'); } - return ZERO.toFixed(precision); + return ZERO.toFixed(finalP); } /** diff --git a/packages/amis/src/renderers/Form/InputNumber.tsx b/packages/amis/src/renderers/Form/InputNumber.tsx index 1ebd5e001de..e171ad69867 100644 --- a/packages/amis/src/renderers/Form/InputNumber.tsx +++ b/packages/amis/src/renderers/Form/InputNumber.tsx @@ -330,6 +330,7 @@ export default class NumberControl extends React.Component< return; } const {kilobitSeparator, prefix} = this.props; + const integer = value > 0 ? Math.floor(value) : Math.ceil(value); let pos = `${value}`.length; if (prefix) { @@ -338,13 +339,13 @@ export default class NumberControl extends React.Component< if (kilobitSeparator) { // 处理有千分符的情况 123,456,789 - const ksLen = Math.floor((`${Math.abs(value)}`.length - 1) / 3); + const ksLen = Math.floor((`${Math.abs(integer)}`.length - 1) / 3); if (ksLen > 0) { pos += ksLen; } } - if (this.input) { + if (this.input && (kilobitSeparator || prefix)) { this.input.setSelectionRange?.(pos, pos); } }