I'm trying to create a currency conversion component, with two fields. Both are editable, so you can type the destination amount or the origin amount.
I tried using react-number-format
, it works fine, but depending on the calculation made, there are too many decimals, I want to limit it to only 2:
I tried using toFixed()
and tried to round it either, but depending on the calculation I enter in an infinite loop, because my handleChange
method is this:
// change amount to send
const handleAmount = value => {
setValues({
...values,
amount: value,
total: value * rates.rates[values.country]
});
};
// change amount to receive
const handleReceive = value => {
setValues({
...values,
total: value,
amount: value / rates.rates[values.country]
});
};
What I want to achieve is something like this, no more than 2 decimals:
Here is the codesandbox with the code that isn't working as expected: https://codesandbox.io/s/zen-khorana-wks08
How can I achieve this?
Thanks in advance