Skip to content

Commit c9046a6

Browse files
author
wanghao
committed
fix: should process 0 correctly when precision is 0
1 parent d3de147 commit c9046a6

File tree

4 files changed

+34
-36
lines changed

4 files changed

+34
-36
lines changed

Diff for: 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

Diff for: 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
/**

Diff for: 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');

Diff for: tests/util.test.tsx

+14-13
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import getMiniDecimal, {
22
BigIntDecimal,
33
DecimalClass,
44
NumberDecimal,
5-
roundDownUnsignedDecimal,
6-
roundUpUnsignedDecimal,
5+
roundUnsignedDecimal,
76
toFixed,
87
ValueType,
98
} from '../src/utils/MiniDecimal';
@@ -154,19 +153,21 @@ describe('InputNumber.Util', () => {
154153
});
155154

156155
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');
156+
expect(roundUnsignedDecimal('0', 0, false)).toEqual('0');
157+
expect(roundUnsignedDecimal('77.89', 1, false)).toEqual('77.8');
158+
expect(roundUnsignedDecimal('77.1', 2, false)).toEqual('77.10');
159+
expect(roundUnsignedDecimal('77.81', 1, false)).toEqual('77.8');
160+
expect(roundUnsignedDecimal('77.50', 1, false)).toEqual('77.5');
161+
expect(roundUnsignedDecimal('77.5999', 0, false)).toEqual('77');
162+
expect(roundUnsignedDecimal('77.0001', 0, false)).toEqual('77');
163163
})
164164
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');
165+
expect(roundUnsignedDecimal('0', 0, true)).toEqual('0');
166+
expect(roundUnsignedDecimal('77.89', 1, true)).toEqual('77.9');
167+
expect(roundUnsignedDecimal('77.81', 1,true)).toEqual('77.9');
168+
expect(roundUnsignedDecimal('77.89', 0,true)).toEqual('78');
169+
expect(roundUnsignedDecimal('77.599', 0,true)).toEqual('78');
170+
expect(roundUnsignedDecimal('77.01', 0,true)).toEqual('78');
170171
})
171172
});
172173
});

0 commit comments

Comments
 (0)