JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q78341F
  • License MIT

Simple reactive number formatter for Svelte + TypeScript inputs

Package Exports

  • svelte-number-formatter
  • svelte-number-formatter/action
  • svelte-number-formatter/utils

Readme

๐Ÿ”ข svelte-number-formatter

A lightweight, reactive number formatter utility for Svelte + TypeScript.

Supports:

  • Live number formatting for inputs (12,345.67, $1,000.00, etc.)
  • Extraction of clean raw values (12345.67)
  • Currency, locale, and decimal formatting
  • Seamless integration with Svelte reactivity

๐Ÿ“ฆ Installation

From npm (public)

npm install svelte-number-formatter

From GitHub Packages

npm install @dev-henen/svelte-number-formatter --registry=https://npm.pkg.github.com

๐Ÿ“ GitHub Repository: github.com/dev-henen/svelte-number-formatter


๐Ÿš€ Getting Started

import { NumberFormatter } from 'svelte-number-formatter';

const formatter = new NumberFormatter();

โœจ Basic Usage in a Svelte Component

<script lang="ts">
    import { NumberFormatter } from 'svelte-number-formatter';

    const formatter = new NumberFormatter();
</script>

<input bind:value={formatter.handler} />

<p>Raw: {formatter.raw}</p>
<p>Formatted: {formatter.formatted}</p>

or, using a local value:

<script lang="ts">
    import { NumberFormatter } from 'svelte-number-formatter';

    const formatter = new NumberFormatter();

    let value = formatter.formatted;

    $: formatter.handler = value;
</script>

<input bind:value={value} />

<p>Raw: {formatter.raw}</p>
<p>Formatted: {formatter.formatted}</p>

โš ๏ธ Important: Don't Pass Format Options When Binding .handler

When binding formatter.handler directly to an <input>:

<input bind:value={formatter.handler} />

โ— Do not pass formatting options to the constructor.

Doing this:

// โš ๏ธ Don't do this when binding handler
const formatter = new NumberFormatter("1234.5", {
    style: 'currency',
    currency: 'USD',
    decimals: 2
});

...will cause the value to be instantly formatted while typing, which leads to a frustrating user experience (e.g., jumping cursors, blocked input).

โœ… Instead, do this:

// โœ… No formatting options when using .handler
const formatter = new NumberFormatter("1234.5");

Or simply:

const formatter = new NumberFormatter();

You can still use .format() or setOptions() later for formatting output.


๐Ÿ”ง API

constructor(initial?: string, options?: FormatOptions)

Create a new formatter with an optional initial value and formatting options (only when not binding handler to an input).

Properties

Property Type Description
handler string Input setter that auto-updates raw + formatted
raw string The numeric value stripped of symbols
formatted string The value formatted for display (12,345.67)

๐Ÿ” Setting .handler = "12345" will automatically update .raw and .formatted.


โš™๏ธ setOptions(options: Partial<FormatOptions>)

Update locale, decimals, grouping, or currency format.

formatter.setOptions({
    style: 'currency',
    currency: 'USD',
    decimals: 2
});

FormatOptions

{
    locale?: string;               // e.g., "en-US"
    useGrouping?: boolean;         // commas (true by default)
    decimals?: number;             // decimal places (default 0)
    currency?: string;             // e.g., "USD"
    style?: 'decimal' | 'currency' // default: 'decimal'
}

๐Ÿงผ reset()

Clear both raw and formatted values.

formatter.reset();

๐ŸŽฏ format(value: string | number): string

Format a raw number programmatically:

formatter.format("123456.789"); // โ†’ "123,456.79" or "$123,456.79"

โœ… Example Use Case

const formatter = new NumberFormatter();

formatter.handler = '456789.123';

console.log(formatter.formatted); // "456,789.12"
console.log(formatter.raw);       // "456789.123"

To format as currency manually:

formatter.setOptions({
    style: 'currency',
    currency: 'USD',
    decimals: 2
});

๐Ÿง  Notes

  • handler acts as a two-way reactive setter for <input> elements.
  • Avoid formatting options when binding directly to input (bind:value={formatter.handler}).
  • Ideal for form fields, dashboards, admin panels, and finance apps.

๐Ÿ“ฅ use:numberFormatter Svelte Action

Easily format <input> values using a Svelte action. Live formatting occurs while typing, and formatting options (e.g., currency) are applied only on blur to avoid cursor issues.

โœ… Usage

<script lang="ts">
    import { numberFormatter } from 'svelte-number-formatter';
    let raw = '';
</script>

<input
    use:numberFormatter={{
        options: {
            style: 'currency',
            currency: 'USD',
            decimals: 2
        },
        onChange: (rawVal, formattedVal) => {
            raw = rawVal;
        }
    }} />

<p>Raw value: {raw}</p>

๐Ÿ“Œ Notes

  • Avoids passing options to constructor โ€” keeps typing smooth and uninterrupted.
  • Formatting options are applied only when the input blurs.
  • onChange(raw, formatted) is triggered on each update.
  • Ideal for declarative usage in forms and custom inputs.

๐Ÿ”ง Parameters

Key Type Description
options FormatOptions Formatting options (applied on blur)
onChange (raw: string, formatted: string) Callback on input change or blur

subscribe(run)

Subscribe to changes in both raw and formatted values (Svelte store interface):

const formatter = new NumberFormatter("12345");

const unsubscribe = formatter.subscribe(({ raw, formatted }) => {
    console.log("Raw:", raw);        // โ†’ "12345"
    console.log("Formatted:", formatted); // โ†’ "12,345"
});

Use this in any reactive context, or bind to values via $formatter.formatted:

<script lang="ts">
    import { NumberFormatter } from 'svelte-number-formatter';
    const formatter = new NumberFormatter("0");
</script>

<input bind:value={$formatter.formatted} />

๐ŸŒ locale is automatically detected from the browser (navigator.language), or defaults to "en-US" if running in SSR or Node environments.



๐Ÿ“œ License

MIT ยฉ dev-henen


๐Ÿ™Œ Coming Soon