Skip to content

Commit bfdcdba

Browse files
committed
add toFixed func
1 parent 3e19a58 commit bfdcdba

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

src/utils/MiniDecimal.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,39 @@ export default function getMiniDecimal(value: ValueType): DecimalClass {
254254
}
255255
return new NumberDecimal(value);
256256
}
257+
258+
/**
259+
* Align the logic of toFixed to around like 1.5 => 2
260+
*/
261+
export function toFixed(numStr: string, separatorStr: string, precision?: number) {
262+
const { negativeStr, integerStr, decimalStr } = trimNumber(numStr);
263+
const precisionDecimalStr = `${separatorStr}${decimalStr}`;
264+
265+
const numberWithoutDecimal = `${negativeStr}${integerStr}`;
266+
267+
if (precision >= 0) {
268+
// We will get last + 1 number to check if need advanced number
269+
const advancedNum = Number(decimalStr[precision]);
270+
271+
if (advancedNum >= 5) {
272+
const advancedDecimal = getMiniDecimal(numStr).add(
273+
`0.${'0'.repeat(precision)}${10 - advancedNum}`,
274+
);
275+
return toFixed(advancedDecimal.toString(), separatorStr, precision);
276+
}
277+
278+
if (precision === 0) {
279+
return numberWithoutDecimal;
280+
}
281+
282+
return `${numberWithoutDecimal}${separatorStr}${decimalStr
283+
.padEnd(precision, '0')
284+
.slice(0, precision)}`;
285+
}
286+
287+
if (precisionDecimalStr === '.0') {
288+
return numberWithoutDecimal;
289+
}
290+
291+
return `${numberWithoutDecimal}${precisionDecimalStr}`;
292+
}

tests/decimal.test.tsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,70 @@ describe('InputNumber.Decimal', () => {
7474
expect(wrapper.find('input').props().value).toEqual('1,1');
7575
expect(onChange).toHaveBeenCalledWith(1.1);
7676
});
77+
78+
describe('precision', () => {
79+
it('decimal step should not display complete precision', () => {
80+
const wrapper = mount(<InputNumber step={0.01} value={2.1} />);
81+
expect(wrapper.findInput().props().value).toEqual('2.10');
82+
});
83+
84+
it('string step should display complete precision', () => {
85+
const wrapper = mount(<InputNumber step="1.000" value={2.1} />);
86+
expect(wrapper.findInput().props().value).toEqual('2.100');
87+
});
88+
89+
it('prop precision is specified', () => {
90+
const onChange = jest.fn();
91+
const wrapper = mount(<InputNumber onChange={onChange} precision={2} defaultValue={2} />);
92+
expect(wrapper.findInput().props().value).toEqual('2.00');
93+
94+
wrapper.changeValue('3.456');
95+
wrapper.blurInput();
96+
expect(onChange).toHaveBeenCalledWith(3.46);
97+
expect(wrapper.findInput().props().value).toEqual('3.46');
98+
99+
onChange.mockReset();
100+
wrapper.changeValue('3.465');
101+
wrapper.blurInput();
102+
expect(onChange).toHaveBeenCalledWith(3.47);
103+
expect(wrapper.findInput().props().value).toEqual('3.47');
104+
105+
onChange.mockReset();
106+
wrapper.changeValue('3.455');
107+
wrapper.blurInput();
108+
expect(onChange).toHaveBeenCalledWith(3.46);
109+
expect(wrapper.findInput().props().value).toEqual('3.46');
110+
111+
onChange.mockReset();
112+
wrapper.changeValue('1');
113+
wrapper.blurInput();
114+
expect(onChange).toHaveBeenCalledWith(1);
115+
expect(wrapper.findInput().props().value).toEqual('1.00');
116+
});
117+
118+
// it('should not trigger onChange when blur InputNumber with precision', () => {
119+
// class Demo extends React.Component {
120+
// onChange = () => {
121+
// onChangeCallCount += 1;
122+
// };
123+
124+
// render() {
125+
// return (
126+
// <InputNumber
127+
// ref="inputNum"
128+
// mergedPPrecision={2}
129+
// defaultValue={2}
130+
// onChange={this.onChange}
131+
// />
132+
// );
133+
// }
134+
// }
135+
// example = ReactDOM.render(<Demo />, container);
136+
// inputNumber = example.refs.inputNum;
137+
// inputElement = ReactDOM.findDOMNode(inputNumber.input);
138+
// Simulate.focus(inputElement);
139+
// Simulate.blur(inputElement);
140+
// expect(onChangeCallCount).to.be(0);
141+
// });
142+
});
77143
});

tests/util.test.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import getMiniDecimal, {
33
DecimalClass,
44
NumberDecimal,
55
ValueType,
6+
toFixed,
67
} from '../src/utils/MiniDecimal';
78

89
jest.mock('../src/utils/supportUtil');
@@ -113,4 +114,26 @@ describe('InputNumber.Util', () => {
113114
expect(new BigIntDecimal('11.28').add('0.0903').toString()).toEqual('11.3703');
114115
});
115116
});
117+
118+
describe('toFixed', () => {
119+
it('less than precision', () => {
120+
expect(toFixed('1.1', ',', 2)).toEqual('1,10');
121+
expect(toFixed('1.23', '.', 5)).toEqual('1.23000');
122+
});
123+
124+
it('large than precision', () => {
125+
expect(toFixed('1.234', '.', 2)).toEqual('1.23');
126+
expect(toFixed('1.235', '.', 2)).toEqual('1.24');
127+
expect(toFixed('1.238', '.', 2)).toEqual('1.24');
128+
129+
// Integer
130+
expect(toFixed('1.238', '.', 0)).toEqual('1');
131+
expect(toFixed('1.5', '.', 0)).toEqual('2');
132+
});
133+
134+
it('empty precision', () => {
135+
expect(toFixed('1.2', '.')).toEqual('1.2');
136+
expect(toFixed('1.000', '.')).toEqual('1');
137+
});
138+
});
116139
});

0 commit comments

Comments
 (0)