Value types
Numora works with strings. Every change gives onValueChange two of them, the raw value for math and the formatted display value for the UI.
The raw value format
The raw value is the one to store and do math on. It has one format, and the locale and the separators do not change it:
- ASCII digits
0to9, even if the user types Arabic-Indic or fullwidth digits .as the decimal mark, even if the field shows,- a leading
-for a negative number, which needsallowNegative - no thousand separators and no spaces
| Options | The field shows | Raw value |
|---|---|---|
| none | 1,234,567.89 | "1234567.89" |
| locale: 'de-DE' | 1.234.567,89 | "1234567.89" |
| locale: 'fr-FR' | 1 234 567,89 | "1234567.89" |
| locale: 'en-IN' | 12,34,567.89 | "1234567.89" |
| locale: 'ar-EG' | 1٬234٬567٫89 | "1234567.89" |
The raw value is what the user has typed so far, so it is not always a complete number. It can be "" (empty field), "-", ".", "1." or ".5". Check for these before you parse it. Numora keeps trailing zeros, so 0.50 stays "0.50". With decimalMinLength it pads on blur and when your app sets the value, and the padding is part of the raw value, so 5 becomes "5.00". A blur that pads the value or strips a trailing separator changes the raw value, so it calls onValueChange. On blur "-", "." and "-." reset to "", and "-0" becomes "0".
defaultValue and setValue take this same format, so the value you get from onValueChange can go straight back into setValue.
Raw value and formatted value
onValueChange receives { value, formattedValue }. value is the raw value described above and formattedValue is the text the field shows. instance.rawValue and instance.formattedValue return the same two strings. The naming rule is that the instance says rawValue, and callbacks and options say value.
Type below. Both readouts update on every keystroke. The field is built with numora-react, and a vanilla NumoraInput reports the same two values:
Native form submission
A plain <form> submit or new FormData(form) reads the input's DOM value, which is the display string. That is "1,234.56", or "1.234,56" in de-DE. To submit the raw value, leave the visible input without a name and copy the raw value into a hidden input:
When you need a number
Numora has no numeric getter. If you need a JavaScript number, convert the raw value yourself. Only do it where a rounding error is acceptable, such as a chart or a slider:
Number is an IEEE 754 double. Integers above Number.MAX_SAFE_INTEGER and long decimals lose precision, and JavaScript raises no error when they do. For financial arithmetic, keep working with the string from onValueChange and use a decimal library such as decimal.js, or parseUnits for token amounts.
Writing a value
rawValue is read-only. To set the field from code, call setValue with a canonical raw value. Under locale: 'de-DE', setValue('1234.5') displays 1.234,5.
setValue is your own write, so it does not call onValueChange and isAllowed does not check it. The new text still goes to onFormattedValueChange. Numora reads a , in a value you pass in as a decimal mark, like ., and never as grouping. So setValue('1234,5') sets the same number and logs a warning in development. Turn formatted text into a raw value with parseValue() first.