From 705e6fc743564a035dc23fc8dc5b116bbb9bf3db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=98=B6?= Date: Fri, 22 Jul 2022 08:38:35 +0800 Subject: [PATCH] feat: number precision (#661) * feat: number precision * feat: add test cases Co-authored-by: chenos --- .../antd/input-number/ReadPretty.tsx | 12 ++++++-- .../__tests__/toFixedByStep.test.ts | 30 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 packages/core/client/src/schema-component/antd/input-number/__tests__/toFixedByStep.test.ts diff --git a/packages/core/client/src/schema-component/antd/input-number/ReadPretty.tsx b/packages/core/client/src/schema-component/antd/input-number/ReadPretty.tsx index 40505d140..3c6db75e2 100644 --- a/packages/core/client/src/schema-component/antd/input-number/ReadPretty.tsx +++ b/packages/core/client/src/schema-component/antd/input-number/ReadPretty.tsx @@ -5,16 +5,24 @@ import { toFixed } from 'rc-input-number/lib/utils/MiniDecimal'; import { getNumberPrecision } from 'rc-input-number/lib/utils/numberUtil'; import React from 'react'; +export function toFixedByStep(value: any, step: string | number) { + if (typeof value === 'undefined' || value === null || value === '') { + return ''; + } + const precision = getNumberPrecision(step); + // return parseFloat(String(value)).toFixed(precision); + return toFixed(String(value), '.', precision); +} + export const ReadPretty: React.FC = (props: any) => { const { step, value, addonBefore, addonAfter } = props; if (!isValid(props.value)) { return
; } - const precision = Math.max(getNumberPrecision(String(value)), getNumberPrecision(step)); return (
{addonBefore} - {toFixed(String(value), '.', precision)} + {toFixedByStep(value, step)} {addonAfter}
); diff --git a/packages/core/client/src/schema-component/antd/input-number/__tests__/toFixedByStep.test.ts b/packages/core/client/src/schema-component/antd/input-number/__tests__/toFixedByStep.test.ts new file mode 100644 index 000000000..c2fcc010a --- /dev/null +++ b/packages/core/client/src/schema-component/antd/input-number/__tests__/toFixedByStep.test.ts @@ -0,0 +1,30 @@ +import { toFixedByStep } from '../ReadPretty'; + +describe('toFixedByStep', () => { + it('less than precision', () => { + expect(toFixedByStep('1.1', '1.00')).toEqual('1.10'); + expect(toFixedByStep('1.23', '1.00000')).toEqual('1.23000'); + }); + + it('large than precision', () => { + expect(toFixedByStep('1.234', '1.00')).toEqual('1.23'); + expect(toFixedByStep('1.235', '1.00')).toEqual('1.24'); + expect(toFixedByStep('1.238', '1.00')).toEqual('1.24'); + expect(toFixedByStep('1.15', '1.0')).toEqual('1.2'); + expect(toFixedByStep('0.15', '1.0')).toEqual('0.2'); + + // Integer + expect(toFixedByStep('1.238', 1)).toEqual('1'); + expect(toFixedByStep('1.5', 1)).toEqual('2'); + }); + + it('should return "" when input is ""/null/undefined', () => { + expect(toFixedByStep('', '1.00')).toEqual(''); + expect(toFixedByStep(null, '1.00')).toEqual(''); + expect(toFixedByStep(undefined, '1.00')).toEqual(''); + }); + + it('negative', () => { + expect(toFixedByStep('-77.88', '1.0')).toEqual('-77.9'); + }); +});