Skip to content

Introduction to ts-numbers ​

Social Card of ts-numbers

ts-numbers is a powerful yet lightweight TypeScript library for formatting and parsing numbers with support for currencies, specialized formats, and internationalization.

Key Features ​

  • Versatile Number Formatting: Control decimal places, separators, rounding methods, and more
  • Currency Support: Format monetary values with proper currency symbols and international formats
  • Specialized Types: Format phone numbers, IP addresses, time, temperature, and more
  • Input Handling: Comprehensive event handling for user input with automatic formatting
  • Internationalization: Support for different locales, numbering systems, and RTL text
  • Unit Conversion: Built-in conversion for temperature, weight, length, and other measurements
  • Scientific Notation: Format very large or small numbers with scientific notation
  • Advanced Features: Presets, extended persistence, style rules, and more

Why ts-numbers ​

ts-numbers is designed to solve the complex problem of number formatting and input handling in web applications. It provides a consistent and powerful API for handling numeric values with the following benefits:

  • Type Safety: Built from the ground up with TypeScript for excellent type safety
  • Lightweight: Small bundle size with no external dependencies
  • Comprehensive: Handles a wide range of number formatting needs
  • Flexible: Works with any HTML input element or element that can display text
  • Extensible: Easy to customize and extend with your own formatting rules
  • Well-tested: Extensive test coverage for reliability

Quick Example ​

typescript
import { Numbers } from 'ts-numbers'

// Basic currency formatting
const price = new Numbers('#price-input', {
  currencySymbol: '$',
  decimalPlaces: 2,
  digitGroupSeparator: ',',
})

// Set a value programmatically
price.set(1234.56) // Displays as "$1,234.56"

// Get the raw numeric value
const value = price.getNumber() // 1234.56

Ready to get started? Check out the Installation guide next.

Get Started ​

It's rather simple to get your package development started:

bash
# you may use this GitHub template or the following command
bunx degit stacksjs/ts-numbers my-pkg
cd my-pkg

# if you don't have pnpm installed, run `npm i -g pnpm`
bun i # install all deps
bun run build # builds the library for production-ready use

# after you have successfully committed, you may create a "release"
bun run release # automates git commits, versioning, and changelog generations

Check out the package.json scripts for more commands.

Developer Experience (DX) ​

This Starter Kit comes pre-configured with the following:

Changelog ​

Please see our releases page for more information on what has changed recently.

Stargazers ​

Stargazers

Contributing ​

Please review the Contributing Guide for details.

Community ​

For help, discussion about best practices, or any other conversation that would benefit from being searchable:

Discussions on GitHub

For casual chit-chat with others using this package:

Join the Stacks Discord Server

Postcardware ​

Two things are true: Stacks OSS will always stay open-source, and we do love to receive postcards from wherever Stacks is used! 🌍 We also publish them on our website. And thank you, Spatie

Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094

Sponsors ​

We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.

Credits ​

License ​

The MIT License (MIT). Please see LICENSE for more information.

Made with 💙