A numeric input library for the web. It keeps every value as a string, so an amount never passes through floating point. It has no runtime dependencies and works with any framework or none.
| Package | Version | Description |
|---|---|---|
numora |
Core, vanilla JS/TypeScript | |
numora-react |
React component wrapper |
# Vanilla JS/TypeScript
npm install numora
# React (includes numora)
npm install numora-reactimport { NumoraInput } from 'numora';
const target = document.querySelector<HTMLInputElement>('#amount');
if (target) {
const input = new NumoraInput(target, {
decimalMaxLength: 2,
onValueChange: ({ value, formattedValue }) => console.log(value, formattedValue),
});
input.rawValue; // canonical raw value, e.g. '1234.5'
}import { NumoraInput } from 'numora-react';
<NumoraInput decimalMaxLength={2} onValueChange={({ value }) => console.log(value)} />Format or parse without an input:
import { formatValue, parseValue } from 'numora';
formatValue('1234.5', { locale: 'de-DE', decimalMinLength: 2 }); // '1.234,50'
parseValue('1.234,50', { locale: 'de-DE' }); // '1234.50'Coming from v4? See MIGRATION.md.
- Sanitization removes non-numeric characters and mobile keyboard artifacts
- Formatting adds thousand separators in Thousand, Lakh or Wan grouping, on blur or on every keystroke
- Decimals sets the minimum and maximum decimal places and the decimal separator
- Compact notation is opt-in (
allowCompactNotation) and expands "1k" → "1000" and "1.5m" → "1500000" when typed, pasted or passed tosetValue. A suffix in the middle of a number, such as "1k5", does not scale the prefix - Scientific notation expands "1.5e-7" → "0.00000015"
- Leading zeros are stripped by default, and
allowLeadingZeroskeeps them
Numora detects the format of pasted text. If . and , both appear, the last one is the decimal. A mark that repeats is grouping. A single mark is the decimal when it is the field's own decimal separator. Otherwise it is grouping when the digits around it form valid groups (1,234, but not 0,123 or 1234,567), and the decimal when it does not. So "1.234,56" pasted into an en-US field is 1,234.56. Numora never runs this detection on typed keys or on values the app passes in. Dropped and autofilled text is detected the same way, except in a focused field that formats on change (the default), which reads it as its own display text.
The full documentation and a live demo are at numeric-input.com.
MIT - Kacper Szarkiewicz