I'd like to keep the two digits after a number ie 2.89
or 2.00
. Google brought me to this answer to use .toFixed(2)
.
While that works great, it does not work well when entered input
values:
const [ value, setValue] = useState()
const onChange = (value) => {
const float = parseFloat(value)
setValue(float.toFixed(2))
}
<input
type="number"
value={value}
onChange={({ target }) => onChange(target.value)}
/>
If I should type, say, "300", the input value stops at "3.00". I have to move the cursor before "3" to type "300". What's the best way to do this?
I expect the value to always show .33
, .00
etc while having the ability to "free type". As I type this question, I feel I need to use onBlur
to convert the value to .toFixed
and not while typing?