-
Notifications
You must be signed in to change notification settings - Fork 0
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
format registered number input AssetContainer #38
Comments
Some native features of use-hook-form: https://react-hook-form.com/advanced-usage#TransformandParse They utilize However |
I need to caver range of scenarios. Additional scenario problem: ChackraUI
|
Made regex to capture zeros |
Another problem is JS float precision.
const number = 0.00065934
const numForm = Intl.NumberFormat().format(number) // 0.001
const toLoc = number.toLocaleString() // 0.001 numeral.js increases formatting accuracy to const numString = "0.123456789123456789"
const parseFl = parseFloat(numString) // '0.12345678912345678'
const number = 0.123456789123456789
const numForm = Intl.NumberFormat().format(number) // 0.123
const toLoc = number.toLocaleString() // 0.123
const numeralVar = numeral(number).format('0.[00000000000000000]') // '0.12345678912345678' for more accuracy https://www.npmjs.com/package/decimal.js/v/3.0.0 |
The more complex problem to deal with is carret jumping after updating value with: <Input
{...register(`${assetName}.amount`, {
onChange: (e) => {
const numberFormatted = stringToNumberFormatLocale(
e.target.value
);
setValue(`${assetName}.amount`, numberFormatted); // async function, so carret position resets
},
})}
/> carret.jump.movMight be better to use external libraries for some sort of masks
|
BTW Mantine UI inputs supports this natively Under the hood it uses react-number-format https://github.com/mantinedev/mantine/blob/master/packages/%40mantine/core/src/components/NumberFormatter/NumberFormatter.tsx |
Here is an article how to combine all the libraries (chakra, react-number-format and react-hook-form) https://medium.com/@roquejr1307/chakra-form-hook-number-format-73ff9ed6f84 With a good explanation of hook-form's |
Well, solved number formatting issue with a more complex approach with react-number-format |
Similar to:
Need to transform numbers inside inputs.
10000
->10,000
or10 000
Need support for floating values (i.e. for crypto)
0.00065934
The text was updated successfully, but these errors were encountered: