Skip to content

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 0 to 9, 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 needs allowNegative
  • no thousand separators and no spaces
OptionsThe field showsRaw value
none1,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.

import { NumoraInput } from 'numora'

const numoraInput = new NumoraInput(container, {
  onValueChange: ({ value, formattedValue }) => {
    console.log(value)          // "1234.56" ← no separators
    console.log(formattedValue) // "1,234.56" ← display value
  },
})
numoraInput.rawValue       // "1234.56"
numoraInput.formattedValue // "1,234.56"

Type below. Both readouts update on every keystroke. The field is built with numora-react, and a vanilla NumoraInput reports the same two values:

formattedValue
"1,234,567.89"
value
"1234567.89"

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:

const hidden = form.querySelector<HTMLInputElement>('input[name="amount"]')!
const numoraInput = new NumoraInput(container, {
  onValueChange: ({ value }) => {
    hidden.value = value // "1234.56"
  },
})
// onValueChange does not fire for the initial value or for setValue(),
// so copy the raw value yourself after those.
hidden.value = numoraInput.rawValue

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:

const numoraInput = new NumoraInput(container, { defaultValue: '1234.56' })

const num = Number(numoraInput.rawValue)
console.log(num)                                // 1234.56
console.log(numoraInput.rawValue === '')        // true when the input is empty

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.