Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 214b53e

Browse files
author
wanghao
committedJan 1, 2022
fix: should process 0 correctly when precision is 0
1 parent d3de147 commit 214b53e

File tree

4 files changed

+50
-36
lines changed

4 files changed

+50
-36
lines changed
 

‎src/InputNumber.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import KeyCode from 'rc-util/lib/KeyCode';
44
import { composeRef } from 'rc-util/lib/ref';
55
import getMiniDecimal, {
66
DecimalClass,
7-
roundDownUnsignedDecimal,
8-
roundUpUnsignedDecimal,
7+
roundUnsignedDecimal,
98
toFixed,
109
ValueType
1110
} from './utils/MiniDecimal';
@@ -46,11 +45,11 @@ const getDecimalIfValidate = (value: ValueType, precision: number | undefined, i
4645

4746
const {negative, integerStr, decimalStr, negativeStr} = trimNumber(decimal.toString());
4847
const unSignedNumberStr = integerStr +'.' + decimalStr;
49-
48+
5049
if ((isMax && !negative) || (!isMax && negative)) {
51-
return getMiniDecimal(negativeStr + roundDownUnsignedDecimal(unSignedNumberStr, precision));
50+
return getMiniDecimal(negativeStr + roundUnsignedDecimal(unSignedNumberStr, precision, false));
5251
} else {
53-
return getMiniDecimal(negativeStr + roundUpUnsignedDecimal(unSignedNumberStr, precision));
52+
return getMiniDecimal(negativeStr + roundUnsignedDecimal(unSignedNumberStr, precision, true));
5453
}
5554
};
5655

‎src/utils/MiniDecimal.ts

+11-18
Original file line numberDiff line numberDiff line change
@@ -266,27 +266,20 @@ export default function getMiniDecimal(value: ValueType): DecimalClass {
266266

267267
/**
268268
* round up an unsigned number str, like: 1.4 -> 2, 1.5 -> 2
269+
* Or round down an unsigned number str, like: 1.4 -> 1, 1.5 -> 1
269270
*/
270-
export function roundUpUnsignedDecimal(numStr: string, precision: number) {
271+
export function roundUnsignedDecimal(numStr: string, precision: number, roundUp: boolean) {
271272
const {integerStr, decimalStr} = trimNumber(numStr);
272-
const advancedDecimal = getMiniDecimal(integerStr + '.' + decimalStr).add(
273-
`0.${'0'.repeat(precision)}${5}`,
274-
);
275-
return toFixed(advancedDecimal.toString(), '.', precision);
276-
}
277273

278-
/**
279-
* round up an unsigned number str, like: 1.4 -> 1, 1.5 -> 1
280-
*/
281-
export function roundDownUnsignedDecimal(numStr: string, precision: number) {
282-
const {negativeStr, integerStr, decimalStr} = trimNumber(numStr);
283-
const numberWithoutDecimal = `${negativeStr}${integerStr}`;
284-
if (precision === 0) {
285-
return integerStr;
286-
}
287-
return `${numberWithoutDecimal}.${decimalStr
288-
.padEnd(precision, '0')
289-
.slice(0, precision)}`;
274+
// round up decimal part
275+
const times = Math.pow(10, precision);
276+
277+
const roundFn = roundUp ? Math.ceil : Math.floor;
278+
const decimalPart = roundFn(parseFloat(`0.${decimalStr}`) * times) / times;
279+
// add decimal part and integer part
280+
const advancedDecimal = getMiniDecimal(integerStr).add(decimalPart);
281+
282+
return toFixed(advancedDecimal.toString(), '.', precision);
290283
}
291284

292285
/**

‎tests/input.test.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ describe('InputNumber.Input', () => {
5959
expect(wrapper.getInputValue()).toEqual('-98');
6060
});
6161

62+
it('input zero and precision is 0', () => {
63+
const wrapper = prepareWrapper('0', {min: 0, precision: 0});
64+
expect(wrapper.getInputValue()).toEqual('0');
65+
});
66+
6267
it('negative min with higher precision', () => {
6368
const wrapper = prepareWrapper('-4', {min: -3.5, precision: 0});
6469
expect(wrapper.getInputValue()).toEqual('-3');

‎tests/util.test.tsx

+30-13
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import getMiniDecimal, {
22
BigIntDecimal,
33
DecimalClass,
44
NumberDecimal,
5-
roundDownUnsignedDecimal,
6-
roundUpUnsignedDecimal,
5+
roundUnsignedDecimal,
76
toFixed,
87
ValueType,
8+
lessEquals,
99
} from '../src/utils/MiniDecimal';
1010

1111
jest.mock('../src/utils/supportUtil');
@@ -154,19 +154,36 @@ describe('InputNumber.Util', () => {
154154
});
155155

156156
it('round down', () => {
157-
expect(roundDownUnsignedDecimal('77.89', 1)).toEqual('77.8');
158-
expect(roundDownUnsignedDecimal('77.1', 2)).toEqual('77.10');
159-
expect(roundDownUnsignedDecimal('77.81', 1)).toEqual('77.8');
160-
expect(roundDownUnsignedDecimal('77.50', 1)).toEqual('77.5');
161-
expect(roundDownUnsignedDecimal('77.5999', 0)).toEqual('77');
162-
expect(roundDownUnsignedDecimal('77.0001', 0)).toEqual('77');
157+
expect(roundUnsignedDecimal('0', 0, false)).toEqual('0');
158+
expect(roundUnsignedDecimal('77.89', 1, false)).toEqual('77.8');
159+
expect(roundUnsignedDecimal('77.1', 2, false)).toEqual('77.10');
160+
expect(roundUnsignedDecimal('77.81', 1, false)).toEqual('77.8');
161+
expect(roundUnsignedDecimal('77.50', 1, false)).toEqual('77.5');
162+
expect(roundUnsignedDecimal('77.5999', 0, false)).toEqual('77');
163+
expect(roundUnsignedDecimal('77.0001', 0, false)).toEqual('77');
163164
})
165+
164166
it('round up', () => {
165-
expect(roundUpUnsignedDecimal('77.89', 1)).toEqual('77.9');
166-
expect(roundUpUnsignedDecimal('77.81', 1)).toEqual('77.9');
167-
expect(roundUpUnsignedDecimal('77.89', 0)).toEqual('78');
168-
expect(roundUpUnsignedDecimal('77.599', 0)).toEqual('78');
169-
expect(roundUpUnsignedDecimal('77.01', 0)).toEqual('78');
167+
expect(roundUnsignedDecimal('0', 0, true)).toEqual('0');
168+
expect(roundUnsignedDecimal('77.89', 1, true)).toEqual('77.9');
169+
expect(roundUnsignedDecimal('77.81', 1,true)).toEqual('77.9');
170+
expect(roundUnsignedDecimal('77.89', 0,true)).toEqual('78');
171+
expect(roundUnsignedDecimal('77.599', 0,true)).toEqual('78');
172+
expect(roundUnsignedDecimal('77.01', 0,true)).toEqual('78');
173+
})
174+
175+
it('lessEquals', () => {
176+
expect(new NumberDecimal(3).lessEquals(new NumberDecimal(3))).toBeTruthy();
177+
expect(new NumberDecimal(2).lessEquals(new NumberDecimal(3))).toBeTruthy();
178+
expect(new NumberDecimal(4).lessEquals(new NumberDecimal(3))).toBeFalsy();
179+
})
180+
181+
it ('add operation on an invalid decimal', () => {
182+
expect(new NumberDecimal('XXX').add(3).toNumber()).toEqual(3);
183+
})
184+
185+
it ('add NaN', () => {
186+
expect(new NumberDecimal(10).add(NaN).toNumber()).toEqual(10);
170187
})
171188
});
172189
});

0 commit comments

Comments
 (0)