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.
๐ Links
- ๐ GitHub: https://github.com/dev-henen/svelte-number-formatter
- ๐ฆ NPM: https://www.npmjs.com/package/svelte-number-formatter
- ๐ฆ GitHub Package: https://github.com/dev-henen/svelte-number-formatter/packages
๐ License
MIT ยฉ dev-henen