Skip to content
SharqiewiczPublic

About

Framework-agnostic numeric input library

Topics

Resources

Stars

21 stars

Watchers

1 watching

Forks

Repository files navigation

numora

npm version npm downloads

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.

Packages

Package Version Description
numora npm version Core, vanilla JS/TypeScript
numora-react npm version React component wrapper

Install

# Vanilla JS/TypeScript
npm install numora

# React (includes numora)
npm install numora-react

Quick example

import { 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.

Features

  • 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 to setValue. 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 allowLeadingZeros keeps 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.

Documentation

The full documentation and a live demo are at numeric-input.com.

License

MIT - Kacper Szarkiewicz

About

Framework-agnostic numeric input library

Topics

Resources

Stars

21 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages