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

OnValueChange convert input to wrong number #519

Closed
a414137191 opened this issue Apr 1, 2021 · 7 comments
Closed

OnValueChange convert input to wrong number #519

a414137191 opened this issue Apr 1, 2021 · 7 comments
Labels

Comments

@a414137191
Copy link

a414137191 commented Apr 1, 2021

const useStyles = makeStyles((theme) => ({
  root: {
    '& > *': {
      margin: theme.spacing(1),
    },
  },
}));

function NumberFormatCustom(props) {
  const { inputRef, onChange, ...other } = props;

  return (
    <NumberFormat
      {...other}
      getInputRef={inputRef}
      onValueChange={(values) => {
        onChange({
          target: {
            name: props.name,
            value: values.value,
          },
        });
      }}
      allowNegative={false}
      decimalScale={8}
      isNumericString
    />
  );
}

NumberFormatCustom.propTypes = {
  inputRef: PropTypes.func.isRequired,
  name: PropTypes.string.isRequired,
  onChange: PropTypes.func.isRequired,
};

export default function FormattedInputs() {
  const classes = useStyles();
  const [amount1, setAmount1] = React.useState("0");
  const [amount2, setAmount2] = React.useState("0");

  const handleChange = (event) => {
    if(event.target.name === "amount1") {
      setAmount1(event.target.value)
      console.log("change1"+event.target.value)
    }else if(event.target.name === "amount2"){
      setAmount2(event.target.value)
      console.log("change2"+event.target.value)
  }
  };

  return (
    <div className={classes.root}>
      <TextField
        value={amount1}
        label="amount1"
        name="amount1"
        onChange={handleChange}
        InputProps={{
            inputComponent: NumberFormatCustom,
        }}
    />
      <TextField
        label="amount1"
        value={amount2}
        onChange={handleChange}
        name="amount2"
        InputProps={{
          inputComponent: NumberFormatCustom,
        }}
      />
    </div>
  );
}

when i use NumberFormat with decimalScale={8} and enter 997.00000002, it will convert to 1088.

@a414137191 a414137191 changed the title OnValueChange convert to wrong number OnValueChange convert input to wrong number Apr 1, 2021
@alvarotrigo
Copy link

I don't think @s-yadav maintains this project anymore?

@s-yadav
Copy link
Owner

s-yadav commented Apr 6, 2021

I do maintain it @alvarotrigo. Just not getting an active time for it.
@a414137191 can you create a sandbox with breaking case.

@s-yadav
Copy link
Owner

s-yadav commented Apr 6, 2021

@nikhil-varma. looks related to #506, but float values are not used here. @alvarotrigo a codesandbox will help us to debug better.

@a414137191
Copy link
Author

@nikhil-varma. looks related to #506, but float values are not used here. @alvarotrigo a codesandbox will help us to debug better.

https://codesandbox.io/s/number-format-hiitb?file=/package.json

@s-yadav
Copy link
Owner

s-yadav commented Apr 7, 2021

Thanks, @a414137191. Will check.

@s-yadav s-yadav added the bug label Apr 9, 2021
@s-yadav
Copy link
Owner

s-yadav commented Apr 9, 2021

I am able to reproduce and found the root cause. So the real culprit is calling .toString() on the number which does not handle the scientific notation properly.

Looking into the best option to convert scientific notation to normal decimals. Will fix the issue soon.
As the method available on JS for this purpose doesn't properly work for this use case, using a custom function might be a solution.

Something like https://codereview.stackexchange.com/questions/243164/convert-exponential-e-notation-numbers-to-decimals

s-yadav added a commit that referenced this issue Apr 10, 2021
@s-yadav s-yadav mentioned this issue Apr 10, 2021
6 tasks
s-yadav added a commit that referenced this issue Apr 10, 2021
s-yadav added a commit that referenced this issue Apr 10, 2021
s-yadav added a commit that referenced this issue Apr 11, 2021
@s-yadav
Copy link
Owner

s-yadav commented Apr 11, 2021

@a414137191 This is fixed in 4.5.4. Let me know if still face this issue.

@s-yadav s-yadav closed this as completed Apr 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants