# General Translation Platform: formatCurrency
URL: https://generaltranslation.com/zh/docs/platform/core/reference/utility-functions/formatting/format-currency.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: 无需 GT 实例，即可按区域设置格式化货币值。formatCurrency 的 API 参考。

[`formatCurrency`](/docs/platform/core/reference/gt-class-methods/formatting/format-currency) 是 General Translation core library 提供的一个独立实用函数，用于将数值格式化为本地化货币字符串。它对内置的 `Intl.NumberFormat` API 进行了封装，并使用货币 `style`。

## Overview [#overview]

直接从 `generaltranslation` 导入 `formatCurrency`，调用时传入一个值、货币代码和选项对象即可。它不需要 API Key，也不需要 [GT](/docs/platform/core/reference/gt-class/constructor) 实例。若要使用会继承实例区域设置的实例格式化方式，请改用 [`GT`](/docs/platform/core/reference/gt-class/constructor) 实例上的 [`formatCurrency`](/docs/platform/core/reference/gt-class-methods/formatting/format-currency) 方法。

```typescript
import { formatCurrency } from 'generaltranslation';

const price = formatCurrency(1234.56, 'EUR', { locales: ['de-DE'] });
// "1.234,56 €"
```

签名：

```typescript
formatCurrency(
  value: number,
  currency: string,
  options?: { locales?: string | string[] } & Intl.NumberFormatOptions
): string
```

## 工作方式 [#how-it-works]

* **底层 API。** 使用与 GT 类方法相同的 [`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat)，并将 `style` 设为 `'currency'`。
* **区域设置解析。** 省略 `locales` 时，会回退到库的默认区域设置 `en`。
* **符号位置。** 货币符号、分组分隔符和小数格式都遵循解析后的区域设置。

## 参数 [#parameters]

| 参数                      | 描述                              | 类型                                                            | 可选 | 默认值  |
| ----------------------- | ------------------------------- | ------------------------------------------------------------- | -- | ---- |
| [`value`](#value)       | 要格式化的数值。                        | `number`                                                      | 否  | —    |
| [`currency`](#currency) | ISO 4217 货币代码，例如 `USD` 或 `EUR`。 | `string`                                                      | 否  | —    |
| [`options`](#options)   | 格式化配置，包括目标区域设置。                 | `{ locales?: string \| string[] } & Intl.NumberFormatOptions` | 是  | `{}` |

### `value` [#value]

**类型** `number` · **必填**

要格式化的数值。

### `currency` [#currency]

**类型** `string` · **必填**

ISO 4217 货币代码，例如 `USD`、`EUR` 或 `JPY`。

### `options` [#options]

**类型** `{ locales?: string | string[] } & Intl.NumberFormatOptions` · **可选** · **默认值** `{}`

格式设置。下表列出了已发布的 Core 类型公开的常用货币选项及其在 Core 中的实际默认值。 (有关标准补充信息和运行时特定详情，请参阅 [`Intl.NumberFormat` 构造函数选项](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options)) 。

| 属性                         | 描述                                                                           | 类型                                                                                                                   | 可选 | 默认值                                  |
| -------------------------- | ---------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- | -- | ------------------------------------ |
| `locales`                  | 用于格式化的区域设置。                                                                  | `string \| string[]`                                                                                                 | 是  | `en`                                 |
| `localeMatcher`            | 区域设置匹配算法。                                                                    | `'lookup' \| 'best fit'`                                                                                             | 是  | `'best fit'`                         |
| `numberingSystem`          | 数字系统，例如 `latn` 或 `arab`。                                                     | `string`                                                                                                             | 是  | `'latn'`                             |
| `style`                    | 数字格式化样式。除非另行覆盖，否则 `formatCurrency` 会提供货币 `style`。                            | `'decimal' \| 'currency' \| 'percent' \| 'unit'`                                                                     | 是  | `'currency'`                         |
| `currency`                 | 格式化器使用的货币代码。除非另行覆盖，否则位置参数 `currency` 会提供此值。                                  | `string`                                                                                                             | 是  | 位置参数 `currency`                      |
| `currencyDisplay`          | 货币的显示方式。                                                                     | `'symbol' \| 'narrowSymbol' \| 'code' \| 'name'`                                                                     | 是  | `'symbol'`                           |
| `currencySign`             | 使用的货币符号。                                                                     | `'standard' \| 'accounting'`                                                                                         | 是  | `'standard'`                         |
| `unit`                     | 单位标识符。当 `style` 为 `'unit'` 时必填。                                              | `string`                                                                                                             | 是  | —                                    |
| `unitDisplay`              | 单位的显示方式。                                                                     | `'short' \| 'narrow' \| 'long'`                                                                                      | 是  | `'short'`                            |
| `minimumFractionDigits`    | 最小小数位数 (0–100) 。                                                             | `number`                                                                                                             | 是  | 货币最小单位的小数位数；紧凑表示法默认值为 `0`            |
| `maximumFractionDigits`    | 最大小数位数 (0–100) ，不得小于 `minimumFractionDigits`。                                | `number`                                                                                                             | 是  | 货币最小单位的小数位数；紧凑表示法默认值为 `0`            |
| `minimumSignificantDigits` | 启用有效数字舍入时的最小有效数字位数 (1–21) 。                                                  | `number`                                                                                                             | 是  | `1`                                  |
| `maximumSignificantDigits` | 启用有效数字舍入时的最大有效数字位数 (1–21) 。                                                  | `number`                                                                                                             | 是  | `21`；紧凑表示法默认值为 `2`                   |
| `roundingPriority`         | 小数位数和有效数字设置的优先级关系。                                                           | `'auto' \| 'morePrecision' \| 'lessPrecision'`                                                                       | 是  | `'auto'`；紧凑表示法默认值为 `'morePrecision'` |
| `roundingMode`             | 舍入模式。                                                                        | `'ceil' \| 'floor' \| 'expand' \| 'trunc' \| 'halfCeil' \| 'halfFloor' \| 'halfExpand' \| 'halfTrunc' \| 'halfEven'` | 是  | `'halfExpand'`                       |
| `roundingIncrement`        | 舍入增量。非默认值要求实际生效的最小和最大小数位数相等，且不能与有效数字舍入或非 `'auto'` 的 `roundingPriority` 结合使用。 | `1 \| 2 \| 5 \| 10 \| 20 \| 25 \| 50 \| 100 \| 200 \| 250 \| 500 \| 1000 \| 2000 \| 2500 \| 5000`                    | 是  | `1`                                  |
| `useGrouping`              | 是否以及何时使用分组分隔符。                                                               | `boolean \| 'always' \| 'auto' \| 'min2'`                                                                            | 是  | `'auto'`；紧凑表示法默认值为 `'min2'`          |
| `notation`                 | 数字表示法。                                                                       | `'standard' \| 'scientific' \| 'engineering' \| 'compact'`                                                           | 是  | `'standard'`                         |
| `compactDisplay`           | 紧凑表示法的显示样式。                                                                  | `'short' \| 'long'`                                                                                                  | 是  | `'short'`                            |
| `signDisplay`              | 何时显示符号。                                                                      | `'auto' \| 'never' \| 'always' \| 'exceptZero' \| 'negative'`                                                        | 是  | `'auto'`                             |
| `trailingZeroDisplay`      | 是否显示末尾的零。                                                                    | `'auto' \| 'stripIfInteger'`                                                                                         | 是  | `'auto'`                             |

当设置 `notation: 'compact'` 且未指定任何小数位或有效数字选项时，实际采用的数字默认值为 `minimumFractionDigits: 0`、`maximumFractionDigits: 0`、`minimumSignificantDigits: 1` 和 `maximumSignificantDigits: 2`。此时，`roundingPriority` 默认为 `'morePrecision'`，`useGrouping` 默认为 `'min2'`。设置 `style: 'unit'` 时还必须提供有效的 `unit`。

## 返回值 [#returns]

**类型** `string`

格式化后的本地化货币字符串值。

## 示例 [#examples]

```typescript
import { formatCurrency } from 'generaltranslation';

// 美元
console.log(formatCurrency(1234.56, 'USD', { locales: 'en-US' }));
// Output: "$1,234.56"

// 欧元，德语区域设置
console.log(formatCurrency(1234.56, 'EUR', { locales: 'de-DE' }));
// Output: "1.234,56 €"

// 日元（无小数位）
console.log(formatCurrency(1234, 'JPY', { locales: 'ja-JP' }));
// Output: "￥1,234"
```

## 注意事项 [#notes]

* 请始终显式传入 `locales` 值，以确保输出正确且结果可预测。
* 可将任意 `Intl.NumberFormatOptions` (例如 `minimumFractionDigits`) 与 `locales` 一并传入，以进一步微调输出结果。

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
