feat: number precision (#661)
* feat: number precision * feat: add test cases Co-authored-by: chenos <chenlinxh@gmail.com>
This commit is contained in:
parent
d14836cfb7
commit
705e6fc743
@ -5,16 +5,24 @@ import { toFixed } from 'rc-input-number/lib/utils/MiniDecimal';
|
|||||||
import { getNumberPrecision } from 'rc-input-number/lib/utils/numberUtil';
|
import { getNumberPrecision } from 'rc-input-number/lib/utils/numberUtil';
|
||||||
import React from 'react';
|
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<InputProps & InputNumberProps> = (props: any) => {
|
export const ReadPretty: React.FC<InputProps & InputNumberProps> = (props: any) => {
|
||||||
const { step, value, addonBefore, addonAfter } = props;
|
const { step, value, addonBefore, addonAfter } = props;
|
||||||
if (!isValid(props.value)) {
|
if (!isValid(props.value)) {
|
||||||
return <div></div>;
|
return <div></div>;
|
||||||
}
|
}
|
||||||
const precision = Math.max(getNumberPrecision(String(value)), getNumberPrecision(step));
|
|
||||||
return (
|
return (
|
||||||
<div className={'nb-read-pretty-input-number'}>
|
<div className={'nb-read-pretty-input-number'}>
|
||||||
{addonBefore}
|
{addonBefore}
|
||||||
{toFixed(String(value), '.', precision)}
|
{toFixedByStep(value, step)}
|
||||||
{addonAfter}
|
{addonAfter}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
@ -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');
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user