Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix decimal scale issue #501

Merged
merged 8 commits into from
Mar 15, 2021
4 changes: 3 additions & 1 deletion dist/react-number-format.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@ function roundToPrecision(numStr, scale, fixedDecimalScale) {
afterDecimal = _splitDecimal.afterDecimal,
hasNagation = _splitDecimal.hasNagation;

var roundedDecimalParts = parseFloat("0.".concat(afterDecimal || '0')).toFixed(scale).split('.');
var floatValue = parseFloat("0.".concat(afterDecimal || '0'));
var floatValueStr = afterDecimal.length <= scale ? floatValue.toString() : floatValue.toFixed(scale);
var roundedDecimalParts = floatValueStr.split('.');
var intPart = beforeDecimal.split('').reverse().reduce(function (roundedStr, current, idx) {
if (roundedStr.length > idx) {
return (Number(roundedStr[0]) + Number(current)).toString() + roundedStr.substring(1, roundedStr.length);
Expand Down
4 changes: 3 additions & 1 deletion dist/react-number-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@
afterDecimal = _splitDecimal.afterDecimal,
hasNagation = _splitDecimal.hasNagation;

var roundedDecimalParts = parseFloat("0.".concat(afterDecimal || '0')).toFixed(scale).split('.');
var floatValue = parseFloat("0.".concat(afterDecimal || '0'));
var floatValueStr = afterDecimal.length <= scale ? floatValue.toString() : floatValue.toFixed(scale);
var roundedDecimalParts = floatValueStr.split('.');
var intPart = beforeDecimal.split('').reverse().reduce(function (roundedStr, current, idx) {
if (roundedStr.length > idx) {
return (Number(roundedStr[0]) + Number(current)).toString() + roundedStr.substring(1, roundedStr.length);
Expand Down
2 changes: 1 addition & 1 deletion dist/react-number-format.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ function roundToPrecision(numStr, scale, fixedDecimalScale) {
afterDecimal = _splitDecimal.afterDecimal,
hasNagation = _splitDecimal.hasNagation;

var roundedDecimalParts = parseFloat("0.".concat(afterDecimal || '0')).toFixed(scale).split('.');
var floatValue = parseFloat("0.".concat(afterDecimal || '0'));
var floatValueStr = afterDecimal.length <= scale ? floatValue.toString() : floatValue.toFixed(scale);
var roundedDecimalParts = floatValueStr.split('.');
var intPart = beforeDecimal.split('').reverse().reduce(function (roundedStr, current, idx) {
if (roundedStr.length > idx) {
return (Number(roundedStr[0]) + Number(current)).toString() + roundedStr.substring(1, roundedStr.length);
Expand Down
4 changes: 3 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export function roundToPrecision(numStr: string, scale: number, fixedDecimalScal

const shoudHaveDecimalSeparator = numStr.indexOf('.') !== -1 && scale;
const {beforeDecimal, afterDecimal, hasNagation} = splitDecimal(numStr);
const roundedDecimalParts = parseFloat(`0.${afterDecimal || '0'}`).toFixed(scale).split('.');
const floatValue = parseFloat(`0.${afterDecimal || '0'}`);
const floatValueStr = afterDecimal.length <= scale ? floatValue.toString() : floatValue.toFixed(scale)
const roundedDecimalParts = floatValueStr.split('.');
const intPart = beforeDecimal.split('').reverse().reduce((roundedStr, current, idx) => {
if (roundedStr.length > idx) {
return (Number(roundedStr[0]) + Number(current)).toString() + roundedStr.substring(1, roundedStr.length);
Expand Down
38 changes: 37 additions & 1 deletion test/library/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,5 +425,41 @@ describe('NumberFormat as input', () => {
shallow(<NumberFormat format="#### #### ####" mask={['D', 'D', 'M', '1', '2', 'Y', 'Y', 'Y']}/>)
}).toThrow()
})

it('should show the right decimal values based on the decimal scale provided', () =>{
nikhil-varma marked this conversation as resolved.
Show resolved Hide resolved
class WrapperComponent extends React.Component {
constructor() {
super ();
this.state = {
value: '123.123'
};
}

onInputChange = inputObj => {
this.setState({value: inputObj.value})
}

render() {
return (
<NumberFormat
name="numberformat"
id="formatted-numberformat-input"
value={this.state.value}
onValueChange={this.onInputChange}
decimalScale={18}
thousandSeparator
prefix={"$"}
isNumericString
/>
)
}
}

const wrapper = mount(<WrapperComponent />)
expect(wrapper.find('input').instance().value).toEqual('$123.123')
wrapper.setState({value: '123.1234'})
console.log(wrapper.find('input').instance().value)
expect(wrapper.find('input').instance().value).toEqual('$123.1234')
})
})
});
});