---
title: Price for Astro
description: "Astro parts for amounts, currency labels, and billing periods."
seo:
    title: Price for Astro | Shadcn & Tailwind CSS
related:
    - Pricing blocks: /blocks/pricing
    - /components/card
sidebar:
    label: "Price"
---

```astro
---
import { Price, PriceUnit, PriceValue } from "@/components/ui/price"
---

<Price>
  <PriceValue price={99} />
  <PriceUnit>month</PriceUnit>
</Price>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/price
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/components/ui/price/format-price-value.ts

```ts
export interface FormatPriceValueOptions {
  currency: string
  locale: string
}

export type PriceValue = number | string | [number, number]
export type DiscountFormat = "percentage" | "amount"

function formatCurrency(value: number, options: FormatPriceValueOptions) {
  return new Intl.NumberFormat(options.locale, {
    style: "currency",
    currency: options.currency,
  }).format(value)
}

export function parseSinglePriceValue(price: PriceValue | null | undefined) {
  if (price === null || price === undefined || price === "") {
    return null
  }

  if (typeof price === "number") {
    return Number.isFinite(price) ? price : null
  }

  if (Array.isArray(price)) {
    return null
  }

  const numericValue = Number(price)

  if (!Number.isNaN(numericValue) && Number.isFinite(numericValue)) {
    return numericValue
  }

  return null
}

export function formatPriceDiscount(
  price: PriceValue | null | undefined,
  compareAt: PriceValue | null | undefined,
  options: FormatPriceValueOptions & {
    format?: DiscountFormat
  }
) {
  const currentPrice = parseSinglePriceValue(price)
  const originalPrice = parseSinglePriceValue(compareAt)

  if (
    currentPrice === null ||
    originalPrice === null ||
    originalPrice <= currentPrice ||
    originalPrice <= 0
  ) {
    return null
  }

  if (options.format === "amount") {
    return formatCurrency(originalPrice - currentPrice, options)
  }

  return `${Math.round(((originalPrice - currentPrice) / originalPrice) * 100)}%`
}

export function formatPriceValue(
  price: PriceValue | null | undefined,
  options: FormatPriceValueOptions
) {
  if (price === null || price === undefined || price === "") {
    return null
  }

  if (typeof price === "number") {
    return formatCurrency(price, options)
  }

  if (Array.isArray(price)) {
    const [minimum, maximum] = price
    return `${formatCurrency(minimum, options)} - ${formatCurrency(maximum, options)}`
  }

  const rangeMatch = price.match(
    /^\s*(-?\d+(?:\.\d+)?)\s*-\s*(-?\d+(?:\.\d+)?)\s*$/
  )

  if (!rangeMatch) {
    return price
  }

  const [, minimum, maximum] = rangeMatch
  return `${formatCurrency(Number(minimum), options)} - ${formatCurrency(Number(maximum), options)}`
}
```

#### src/components/ui/price/index.ts

```ts
export { default as Price } from "./price.astro"
export { default as PriceUnit } from "./price-unit.astro"
export { default as PriceValue } from "./price-value.astro"
```

#### src/components/ui/price/price-unit.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"span">

const { class: className, ...props } = Astro.props
---

<span
  class={cn("text-muted-foreground text-sm leading-tight", className)}
  {...props}
>
  / <slot />
</span>
```

#### src/components/ui/price/price-value.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import { cva, type VariantProps } from "class-variance-authority"

import { cn } from "cn"

import {
  formatPriceDiscount,
  formatPriceValue,
  type DiscountFormat,
  type PriceValue,
} from "./format-price-value"

type Props = VariantProps<typeof variants> &
  HTMLAttributes<"span"> & {
    price?: PriceValue
    compareAt?: PriceValue
    currency?: string
    locale?: string
    variant?: "default" | "sale"
    discountFormat?: DiscountFormat
    showDiscount?: boolean
  }

const variants = cva("leading-tight", {
  variants: {
    variant: {
      default: "text-foreground",
      sale: "text-muted-foreground line-through",
    },
  },
})

const {
  class: className,
  price,
  compareAt,
  currency = "USD",
  locale = "en-US",
  variant = "default",
  discountFormat = "percentage",
  showDiscount = true,
  ...props
} = Astro.props

const formattedPrice = formatPriceValue(price, { currency, locale })
const formattedCompareAt = formatPriceValue(compareAt, { currency, locale })
const formattedDiscount =
  showDiscount && compareAt
    ? formatPriceDiscount(price, compareAt, {
        currency,
        locale,
        format: discountFormat,
      })
    : null
const hasDiscount = Boolean(formattedCompareAt && formattedDiscount)
---

{formattedPrice && (
  <span
    class={cn("inline-flex flex-wrap items-center gap-x-2 gap-y-1", className)}
    {...props}
  >
    <span class={cn(variants({ variant }))}>{formattedPrice}</span>
    {formattedCompareAt && (
      <span class="text-muted-foreground text-sm leading-tight line-through">
        {formattedCompareAt}
      </span>
    )}
    {hasDiscount && (
      <span data-slot="price-badge" class="bg-muted text-muted-foreground rounded-full px-2 py-0.5 text-xs font-medium">
        Save {formattedDiscount}
      </span>
    )}
  </span>
)}
```

#### src/components/ui/price/price.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div class={cn("flex flex-wrap items-center gap-x-2", className)} {...props}>
  <slot />
</div>
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```ts
import { Price, PriceUnit, PriceValue } from "@/components/ui/price"
```

```astro
<Price>
  <PriceValue price={99} />
  <PriceUnit>month</PriceUnit>
</Price>
```

## Examples

```astro
---
import { Price, PriceUnit, PriceValue } from "@/components/ui/price"
---

<Price>
  <PriceValue price={29} currency="EUR" />
  <PriceUnit>month</PriceUnit>
</Price>
```

```astro
---
import { Price, PriceUnit, PriceValue } from "@/components/ui/price"
---

<Price>
  <PriceValue price={79} compareAt={99} />
  <PriceUnit>month</PriceUnit>
</Price>
```

```astro
---
import { Price, PriceUnit, PriceValue } from "@/components/ui/price"
---

<Price>
  <PriceValue price={[50, 100]} />
  <PriceUnit>month</PriceUnit>
</Price>
```

```astro
---
import { Price, PriceValue } from "@/components/ui/price"
---

<Price>
  <PriceValue price={249} compareAt={299} discountFormat="amount" />
</Price>
```

## Notes

- Use `compareAt` to show the original price and automatic savings output.
- Pass `currency` and `locale` when the default `USD` and `en-US` formatting are
  not correct for your project.
- Use `showDiscount={false}` when you want the strike-through price without the
  savings badge.

## API Reference

Read the [installed source](/r/price.json) for the complete prop types.

## When to use Price

Use Price to arrange the amount and its context in a pricing card or product summary. Keep currency, billing interval, and any conditions close to the number. Supply already formatted content through the parts.

## Keyboard and behavior

Price displays text; it does not convert currencies, calculate tax, or process payment. Make a discounted price understandable in the reading order and explain whether an amount recurs.

## Theme and layout

Typography can emphasize the amount while keeping its unit readable. Test long localized amounts and currency symbols. Use theme text tokens so the smaller billing text remains legible.
