搜尋套件、儲存庫與 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

原始碼儲存庫

使用此套件的專案

暫無相關資料。