# Price

> A formatted price with a smaller currency and cents, an optional period, a struck-through old price and a discount tag.

- Kind: ui · Category: Data display · Interactivity: none
- Install: `npx shadcn@latest add @haze/price`
- File: `src/components/haze/ui/price.astro`
- npm dependencies: none
- Registry dependencies: @haze/tag, @haze/utils
- Docs page: https://hazeui.com/ui/price

## Use when
- A plan card, product tile or cart row shows an amount in a currency and locale chosen by the page.
- A sale needs the old price struck through with a computed 'Save 20%' tag and a clear spoken reading.
- A plan is billed per month or year and the period should sit quietly next to the figure.

## Avoid when
- The user types an amount; use price-input.
- The number is a KPI or a trend, not something sold; use stat.
- A whole plan card with features and a button is needed; use a pricing block such as pricing-three-tier.

## Alternatives
- [price-input](https://hazeui.com/ui/price-input.md)
- [stat](https://hazeui.com/ui/stat.md)
- [animated-number](https://hazeui.com/ui/animated-number.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| amount | `number` | 24 | no | Price in major units (12.5 is twelve and a half, not 1250). 0 shows freeLabel instead of a figure. |
| currency | `string` | "USD" | no | ISO 4217 code passed to Intl.NumberFormat in the frontmatter, so the output is the same on every build. |
| locale | `string` | "en-US" | no | BCP 47 locale for symbol placement, separators and digits, for example de-DE. |
| periodLabel | `string` | "/ month" | no | Text after the figure. Empty for a one-off price. Hidden when the price is free. |
| periodSrLabel | `string` |  | no | Spoken form of the period. Defaults to the label with a leading slash turned into 'per'. |
| compareAt | `number` |  | no | Original price, struck through. Only shown when higher than amount. |
| discountLabel | `string \| false` |  | no | Text of the discount tag. Undefined computes 'Save 20%' from compareAt; false hides the tag. |
| saveLabel | `string` | "Save" | no | Word before the computed percent. Translate it together with locale. |
| nowLabel | `string` | "Now" | no | Spoken prefix of the current price when compareAt is set. |
| wasLabel | `string` | "Was" | no | Spoken prefix of the old price. |
| freeLabel | `string` | "Free" | no | Shown instead of the figure when amount is 0. |
| fractionDigits | `number` |  | no | Decimal places. By default whole amounts show none and others use the currency's own count. |
| size | `"sm" \| "md" \| "lg" \| "xl"` | "md" | no | Figure size from 16 to 48 px. Symbol, decimals and period stay smaller. |
| class | `string` |  | no | Extra classes for the wrapper. |

## Accessibility
- The visual figure is hidden from assistive technology and replaced by one sr-only sentence such as "Now $192 per year. Was $240. Save 20%.", so split symbols and digits are never read piecemeal.
- The struck-through price is never the only cue: the spoken text names it as the old price with the 'Was' label.
- Figures use tabular numerals so a column of prices lines up; the discount tag has text, not color alone.
- Pass translated nowLabel, wasLabel, saveLabel, freeLabel and periodSrLabel when the locale is not English.

## Agent prompts

### Install and use

Install the Haze UI component "Price" with `npx shadcn@latest add @haze/price`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import Price from "@/components/haze/ui/price.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/price.md

### Customize with real content

Adapt the Haze price ui for our product. Replace the demo values of these props with our real content: amount, currency, locale, periodLabel, periodSrLabel, compareAt, discountLabel, saveLabel, nowLabel, wasLabel, freeLabel, fractionDigits, size, class. Keep the layout, the heading level rules and these accessibility details: The visual figure is hidden from assistive technology and replaced by one sr-only sentence such as "Now $192 per year. Was $240. Save 20%.", so split symbols and digits are never read piecemeal. The struck-through price is never the only cue: the spoken text names it as the old price with the 'Was' label. Figures use tabular numerals so a column of prices lines up; the discount tag has text, not color alone. Pass translated nowLabel, wasLabel, saveLabel, freeLabel and periodSrLabel when the locale is not English. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using price, check it fits: use it when a plan card, product tile or cart row shows an amount in a currency and locale chosen by the page.; a sale needs the old price struck through with a computed 'save 20%' tag and a clear spoken reading.; a plan is billed per month or year and the period should sit quietly next to the figure.. Avoid it when the user types an amount; use price-input.; the number is a kpi or a trend, not something sold; use stat.; a whole plan card with features and a button is needed; use a pricing block such as pricing-three-tier.. Alternatives: @haze/price-input, @haze/stat, @haze/animated-number.

## Example requests that should lead here
- Show the Pro plan at $24 per month with the price component.
- Display an annual price of $192 with $240 struck through and a computed save tag.
- Render 12,90 EUR in de-DE with a Monat period and a German spoken label.

## Source

```astro
---
import { cn } from "../../../lib/haze/utils";
import Tag from "./tag.astro";

interface Props {
  /** Price in major units as a plain number (12.5 means 12 dollars 50 cents, not 1250). Floats are fine for display. */
  amount?: number;
  /** ISO 4217 currency code, for example USD, EUR or JPY. */
  currency?: string;
  /** BCP 47 locale that decides symbol position, separators and digits. Fixed so server output is deterministic. */
  locale?: string;
  /** Billing period shown after the figure, for example "/ month". Leave empty for a one-off price. */
  periodLabel?: string;
  /** Spoken form of the period. Defaults to the label with a leading "/" turned into "per". */
  periodSrLabel?: string;
  /** Original price shown struck through. It must be higher than amount to count as a discount. */
  compareAt?: number;
  /** Text of the discount tag. Leave undefined to compute "Save 20%" from compareAt; pass false to hide the tag. */
  discountLabel?: string | false;
  /** Word before the computed percent in the discount tag. */
  saveLabel?: string;
  /** Spoken prefix of the current price when compareAt is set. */
  nowLabel?: string;
  /** Spoken prefix of the original price. */
  wasLabel?: string;
  /** Shown in place of the figure when amount is 0. */
  freeLabel?: string;
  /** Decimal places. By default whole amounts show none and other amounts use the currency's own count. */
  fractionDigits?: number;
  size?: "sm" | "md" | "lg" | "xl";
  class?: string;
}

const {
  amount = 24,
  currency = "USD",
  locale = "en-US",
  periodLabel = "/ month",
  periodSrLabel,
  compareAt,
  discountLabel,
  saveLabel = "Save",
  nowLabel = "Now",
  wasLabel = "Was",
  freeLabel = "Free",
  fractionDigits,
  size = "md",
  class: className,
} = Astro.props;

type Part = { type: string; value: string };

const free = amount === 0;
// Amount and compareAt share one digit count: whole when both are whole, otherwise the currency's own.
const hasFraction = !Number.isInteger(amount) || (compareAt !== undefined && !Number.isInteger(compareAt));
const digits = () => fractionDigits ?? (hasFraction ? undefined : 0);

function format(value: number): { text: string; parts: Part[] } {
  try {
    const formatter = new Intl.NumberFormat(locale, {
      style: "currency",
      currency,
      minimumFractionDigits: digits(),
      maximumFractionDigits: digits(),
    });
    return { text: formatter.format(value), parts: formatter.formatToParts(value) };
  } catch {
    const text = `${currency} ${value}`;
    return { text, parts: [{ type: "integer", value: text }] };
  }
}

const now = format(amount);
const was = compareAt !== undefined && amount >= 0 && compareAt > amount ? format(compareAt) : undefined;

// Floor so a 99.6% saving never reads as 100%.
const percent = was && compareAt ? Math.floor((1 - amount / compareAt) * 100) : 0;
function formatPercent(value: number): string {
  try {
    return new Intl.NumberFormat(locale, { style: "percent" }).format(value / 100);
  } catch {
    return `${value}%`;
  }
}
const tagText = discountLabel === false ? "" : (discountLabel ?? (percent > 0 ? `${saveLabel} ${formatPercent(percent)}` : ""));

const period = free ? "" : periodLabel;
const spokenPeriod = period ? (periodSrLabel ?? period.replace(/^\s*\/\s*/, "per ")) : "";
const spoken = [
  was ? `${nowLabel} ${free ? freeLabel : now.text}${spokenPeriod ? ` ${spokenPeriod}` : ""}.` : `${free ? freeLabel : now.text}${spokenPeriod ? ` ${spokenPeriod}` : ""}`,
  was ? `${wasLabel} ${was.text}.` : "",
  tagText && was ? `${tagText}.` : "",
]
  .filter(Boolean)
  .join(" ");

const figure = {
  sm: "text-base/5",
  md: "text-2xl/7",
  lg: "text-4xl/9",
  xl: "text-5xl/none",
};
const minor = {
  sm: "text-xs/4",
  md: "text-sm/5",
  lg: "text-lg/6",
  xl: "text-2xl/7",
};
const struck = { sm: "text-xs/4", md: "text-sm/5", lg: "text-base/6", xl: "text-lg/7" };
---

<div class={cn("flex min-w-0 flex-wrap items-baseline gap-x-2 gap-y-1", className)}>
  <span class="sr-only">{spoken}</span>
  <span aria-hidden="true" class={cn("font-semibold tracking-tight whitespace-nowrap text-foreground tabular-nums", figure[size])}>
    {
      free ? (
        freeLabel
      ) : (
        now.parts.map((part) => (
          <span class={cn(part.type === "currency" || part.type === "fraction" || part.type === "decimal" ? ["font-medium", minor[size]] : undefined)}>
            {part.value}
          </span>
        ))
      )
    }
  </span>
  {period && <span aria-hidden="true" class={cn("whitespace-nowrap text-muted-foreground", minor[size])}>{period}</span>}
  {was && <s aria-hidden="true" class={cn("whitespace-nowrap text-muted-foreground tabular-nums", struck[size])}>{was.text}</s>}
  {was && tagText && <span aria-hidden="true" class="inline-flex"><Tag variant="success" size={size === "sm" ? "sm" : "md"}>{tagText}</Tag></span>}
</div>
```
