# Number Roll

> Animated number counter for React and shadcn: each digit rolls to its new value, formatted for the user's locale.

Tabular digits that roll in the direction of change, formatted with Intl.NumberFormat for any locale. A 9 → 0 carry rolls forward, a value that changes mid-roll redirects, and new places fade in while the rest slide over.

Also known as a number ticker or an animated counter.

- Tier: Free (MIT)
- Interaction: Select
- Page: https://eased.dev/components/number-roll
- Registry: https://eased.dev/r/number-roll.json

## Install

```bash
npx shadcn add https://eased.dev/r/number-roll.json
```

Tabular digits that roll in the direction of change, formatted with `Intl.NumberFormat` so separators, currency and numerals follow the locale.

## When to use

- A number that updates while someone watches: a balance, a price, a live counter, a total that changes as items are added.
- Figures in a stat tile or a dashboard, where a roll shows which way the value moved.
- Any number that needs locale-aware formatting (currency, percent, compact, units) and should animate between values.

## When not to use

- A number that never changes after render: plain text is enough.
- A value someone edits: use a [Number Field](/components/number-field).
- Text that changes, not a number: use [Text Swap](/components/text-swap).

## Usage

```tsx balance.tsx
import { NumberRoll } from "@/components/eased/number-roll/number-roll";

export function Balance({ amount }: { amount: number }) {
  return (
    <p className="text-display-3xl font-semibold">
      <NumberRoll value={amount} format={{ style: "currency", currency: "USD" }} />
    </p>
  );
}
```

## Examples

### Other locales

Grouping, the decimal mark and the numerals come from the locale, and localised numerals roll too.

```tsx
<NumberRoll value={139775.9} locale="hi-IN" format={{ style: "currency", currency: "INR" }} />
<NumberRoll value={48210} locale="ar-EG" />
```

### Compact and percent

Suffixes and symbols swap while the digits roll, so 980K to 1.2M changes the "K" to an "M".

```tsx
<NumberRoll value={followers} format={{ notation: "compact", maximumFractionDigits: 1 }} />
<NumberRoll value={0.42} format={{ style: "percent" }} />
```

### While dragging

Turn the animation off while a slider or a scrub drives the value, so it follows the pointer exactly. The width then changes at once too; otherwise it springs with the digits, so text beside the number glides when it gains or loses a digit.

```tsx
<NumberRoll value={volume} animated={!dragging} />
```

## API reference

### NumberRoll

| Prop | Type | Default | Description |
|---|---|---|---|
| `value` | `number` | | The number. |
| `locale` | `Intl.LocalesArgument` | runtime locale | Grouping, decimal mark and numerals. `locales` is a deprecated alias, kept for one release. |
| `format` | `Intl.NumberFormatOptions` | | Currency, percent, compact, units, fixed decimals. |
| `formatter` | `(value: number) => string` | | Format it yourself; overrides `locale` and `format`. Digits roll, everything else swaps. |
| `direction` | `"auto" \| "up" \| "down"` | `"auto"` | `auto` rolls up as the number's size grows and down as it shrinks, like an odometer. |
| `animated` | `boolean` | `true` | `false` shows changes at once. |
| `transition` | Motion `Transition` | `snappy` | The spring each digit rides. |
| `aria-hidden` | `boolean` | `false` | Hide it when something else already announces the value. |
| `className` | `string` | `""` | Appended to the root's classes. |

`toRollParts(value, { locale, format, formatter })` is exported too: it splits a formatted number into the keyed digit and symbol parts the component renders.

## Accessibility

- The formatted number is read as one string from a visually hidden copy; the rolling faces are hidden from assistive tech.
- It is not a live region. If changes must be announced, announce them from a live region of your own, or set `aria-hidden` and let the surrounding control (a progressbar's value, a label) speak the number.

## Motion

Every digit is a ten-face cylinder on the snappy spring that only turns in the direction of change, so a 9 to 0 carry rolls forward instead of rewinding, and a value that changes mid-roll redirects from where the digit is. New places enter from half a digit's height with a 2px blur on the side they roll from, leaving places exit the other way in 100ms, and the rest slide over to make room. Reduced motion: values change at once, with no roll or slide.

## Notes for AI

Use NumberRoll for any displayed number that changes in place, and pass formatting through `format` and `locale` instead of pre-formatting a string, so separators and numerals match the reader. Reach for `formatter` only for formats `Intl.NumberFormat` cannot make.

Do not key it on the value or remount it to restart the animation; pass the new `value` and it rolls. Set `animated={false}` while a gesture drives the value, and `aria-hidden` when the number is already announced elsewhere.

## Related

- [Badge](/components/badge): counts in a chip, rolled with this component.
- [Progress](/components/progress): a bar whose value rolls.
- [Text Swap](/components/text-swap): animated changes for words.

## Example

`components/eased/number-roll/number-roll-demo.tsx`

```tsx
"use client";

import { useInView } from "motion/react";
import { useEffect, useRef, useState } from "react";
import { Badge } from "../badge/badge";
import { DemoFrame } from "../lib/demo-frame";
import { Segmented } from "../segmented/segmented";
import { NumberRoll } from "./number-roll";

type Range = "7d" | "30d" | "90d";
const RANGES = [
  { value: "7d", label: "7d" },
  { value: "30d", label: "30d" },
  { value: "90d", label: "90d" },
] as const;

const DATA: Record<Range, { revenue: number; delta: number; orders: number; refunds: number; returning: number }> = {
  "7d": { revenue: 12_480.5, delta: 0.084, orders: 214, refunds: 0.012, returning: 0.36 },
  "30d": { revenue: 48_210.4, delta: -0.031, orders: 869, refunds: 0.018, returning: 0.41 },
  "90d": { revenue: 139_775.9, delta: 0.127, orders: 2_406, refunds: 0.015, returning: 0.44 },
};

const LOCALES = [
  { value: "en-US", label: "en-US" },
  { value: "de-DE", label: "de-DE" },
  { value: "hi-IN", label: "hi-IN" },
] as const;
type Locale = (typeof LOCALES)[number]["value"];

export function NumberRollDemo() {
  const [range, setRange] = useState<Range>("30d");
  const [locale, setLocale] = useState<Locale>("en-US");
  const [live, setLive] = useState({ revenue: 0, orders: 0 });
  const root = useRef<HTMLDivElement>(null);
  const inView = useInView(root);

  // A new order every couple of seconds while the card is on screen: the total and the
  // order count tick up, and carries roll forward. The format switch shows grouping change
  // (hi-IN groups lakhs: 1,39,775.90) and digits slide to their new places.
  useEffect(() => {
    if (!inView) return;
    const t = window.setInterval(() => {
      setLive((l) => ({ revenue: l.revenue + 18 + Math.round(Math.random() * 6400) / 100, orders: l.orders + 1 }));
    }, 2200);
    return () => window.clearInterval(t);
  }, [inView]);

  const d = DATA[range];
  const revenue = d.revenue + live.revenue;
  const orders = d.orders + live.orders;
  const currency = { style: "currency", currency: "USD" } as const;

  return (
    <DemoFrame
      title="Revenue"
      actions={
        <Segmented
          aria-label="Range"
          options={RANGES}
          value={range}
          onValueChange={(r) => {
            setRange(r);
            setLive({ revenue: 0, orders: 0 });
          }}
        />
      }
    >
      <div ref={root} className="flex min-h-0 flex-1 flex-col p-4">
        <div className="flex items-center gap-2">
          <Badge tone="success" dot="live">
            Live
          </Badge>
          <p className="text-ui-sm text-text-muted">Net sales, all channels</p>
        </div>

        <p className="mt-3 text-4xl font-semibold text-text tabular-nums @max-[22rem]:text-3xl">
          <NumberRoll value={revenue} locale={locale} format={currency} />
        </p>
        <div className="mt-2 flex items-center gap-2 text-ui-sm text-text-muted">
          <Badge tone={d.delta >= 0 ? "success" : "danger"}>
            <span aria-hidden>{d.delta >= 0 ? "↑" : "↓"}</span>
            <NumberRoll
              value={Math.abs(d.delta)}
              locale={locale}
              format={{ style: "percent", minimumFractionDigits: 1, maximumFractionDigits: 1 }}
            />
          </Badge>
          <span>vs. previous {range === "7d" ? "week" : range === "30d" ? "month" : "quarter"}</span>
        </div>

        <dl className="mt-6 divide-y divide-line-subtle border-y border-line-subtle text-ui-sm">
          <Row label="Orders">
            <NumberRoll value={orders} locale={locale} />
          </Row>
          <Row label="Average order">
            <NumberRoll value={Math.round((revenue / orders) * 100) / 100} locale={locale} format={currency} />
          </Row>
          <Row label="Returning customers">
            <NumberRoll value={d.returning} locale={locale} format={{ style: "percent" }} />
          </Row>
          <Row label="Refund rate">
            <NumberRoll
              value={d.refunds}
              locale={locale}
              format={{ style: "percent", minimumFractionDigits: 1, maximumFractionDigits: 1 }}
            />
          </Row>
        </dl>

        <div className="mt-auto flex items-center justify-between gap-3 pt-4">
          <p className="text-ui-sm text-text-muted">Format</p>
          <Segmented aria-label="Number format" options={LOCALES} value={locale} onValueChange={setLocale} />
        </div>
      </div>
    </DemoFrame>
  );
}

function Row({ label, children }: { label: string; children: React.ReactNode }) {
  return (
    <div className="flex h-9 items-center justify-between gap-3">
      <dt className="text-text-secondary">{label}</dt>
      <dd className="text-text tabular-nums">{children}</dd>
    </div>
  );
}
```
