搜索插件、代码仓与 APP 分类

插件详情v2.3.0

currency_formatter

一个用于轻松格式化货币的包。它支持设置自定义货币符号和格式,使用一些内置的主流货币格式,或使用系统自带的格式。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.4万
喜欢数
50
Pub 分数
150

安装插件

flutter pub add currency_formatter

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个简化货币值格式化的 Flutter 包。它允许用户将数字格式化为货币,支持自定义符号、主要货币的预设格式或系统默认的货币设置。非常适合需要一致且本地化金钱显示的应用程序,例如电子商务、金融应用或费用跟踪器。该包轻量且兼容所有主要平台,确保统一的用户体验。

适合场景

  • 在电子商务应用中显示价格
  • 在预算工具中格式化支出
  • 在国际应用中本地化货币
  • 在财务仪表板中显示交易金额
  • 自定义多区域应用中的货币符号

主要特点

  • 支持自定义货币符号
  • 包含主要货币的内置格式
  • 默认使用系统定义的货币
  • 跨平台兼容性
  • 用于格式化数字的简单 API
  • 可配置的小数和千位分隔符

适合对象

  • 开发金融或购物应用的开发者
  • 面向多个地区的应用
  • 需要本地化货币显示的项目
  • 希望在货币格式设置上尽可能简单的团队
  • 偏好原生货币格式的用户

使用建议

  • 需要手动处理货币代码映射
  • 仅限格式化;不解析或验证输入
  • 可能需要额外的逻辑来处理双向货币转换
  • 取决于系统货币回退的区域设置
  • 未针对实时汇率更新进行优化

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

currency_formatter

A package to easily format money. It supports setting a custom currency symbol and format, using some of the inbuilt ones for the main currencies or using the system one.

Format money

To set how a double will be formatted, create a CurrencyFormat:

CurrencyFormat euroSettings = CurrencyFormat(
  code: 'eur',
  symbol: '€',
  symbolSide: SymbolSide.left,
  thousandSeparator: '.',
  decimalSeparator: ',',
  symbolSeparator: ' ',
);
  • symbolSide can be set to SymbolSide.left, SymbolSide.right, or SymbolSide.none.
  • thousandSeparator and decimalSeparator default to '.', ',' or ',','.' automatically depending on symbolSide.
  • negativeSignPlacement can be set to NegativeSignPlacement.beforeSymbol or NegativeSignPlacement.afterSymbol, and defaults to NegativeSignPlacement.afterSymbol.

To format a num, CurrencyFormatter.format() is used:

num amount = 1910.9347;
String formatted = CurrencyFormatter.format(amount, euroSettings); // 1.910,93 €
String compact = CurrencyFormatter.format(amount, euroSettings, compact: true); // 1,91K €
String threeDecimal = CurrencyFormatter.format(amount, euroSettings, decimal: 3); // 1.910,945 €

Parse formatted money

To get a num from a formatted String, use CurrencyFormatter.parse():

// Above's example continuation
num parseFormatted = CurrencyFormatter.parse(formatted, euroSettings); // 1910.93
num parseCompact = CurrencyFormatter.parse(compact, euroSettings); // 1910.0
num parseThreeDecimal = CurrencyFormatter.parse(threeDecimal, euroSettings); // 1910.935

Predefined CurrencyFormats

Predefined settings for many currencies are also included in this package. They are listed in the CurrencyFormatter.majorsList list and defined as static constants in the CurrencyFormat class.

String inUSD = CurrencyFormatter.format(amount, CurrencyFormat.usd); // $ 1,910.93
String inRUB = CurrencyFormatter.format(amount, CurrencyFormatter.fromCode('rub')!); // 1.910,93 ₽

String jpySymbol = CurrencyFormatter.jpy.symbol; // ¥
Get local CurrencyFormat

In Flutter, you can get the default CurrencyFormat according to the device language using CurrencyFormat.local:

String inSystemCurrency = CurrencyFormatter.format(amount, CurrencyFormat.local ?? CurrencyFormat.usd);

If instead you want to get the CurrencyFormat for a specific locale, use CurrencyFormat.fromLocale():

String inLocaleCurrency = CurrencyFormatter.format(amount, CurrencyFormat.fromLocale('en_US')!);
Get CurrencyFormat from symbol or code

You can get a CurrencyFormat from a currency symbol (if it is in CurrencyFormatter.majors) with CurrencyFormat.fromSymbol():

String fromSymbol = CurrencyFormatter.format(amount, CurrencyFormat.fromSymbol('£')!); // £ 1,910.35

String fromCode = CurrencyFormatter.format(amount, CurrencyFormat.fromCode('gbp')!); // £ 1,910.35

Using custom currencies

Let's say you want to use the following custom currency seamlessly:

CurrencyFormat khr = CurrencyFormat(
  symbol: '៛',
  symbolSide: SymbolSide.right,
);

You can create your own majors list:

const List<CurrencyFormat> myCurrencies = [
  ...CurrencyFormatter.majorsList,
  khr,
];

And use it to get the currency corresponding to a symbol or code, or the local one:

CurrencyFormat.fromSymbol('៛', myCurrencies); // khr
CurrencyFormat.fromCode('khr', myCurrencies); // khr

// custom local currency
CurrencyFormat? localCurrency() =>
    CurrencyFormat.fromSymbol(CurrencyFormat.localSymbol, myCurrencies);
// or
CurrencyFormat? localCurrency() =>
    CurrencyFormat.fromLocale(null, myCurrencies);

所属分类

依赖

依赖清单 2 项

  • intl>=0.17.0 <0.21.0
  • web>=1.0.0 <=3.0.0

源码仓库

使用此插件的项目

暂无相关数据。