currency_formatter 2.3.0 copy "currency_formatter: ^2.3.0" to clipboard
currency_formatter: ^2.3.0 copied to clipboard

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.

example/example.dart

import 'package:currency_formatter/currency_formatter.dart';

void main() {
  const CurrencyFormat euroSettings = CurrencyFormat(
    // formatter settings for euro
    code: 'eur',
    symbol: '€',
    symbolSide: SymbolSide.right,
    thousandSeparator: '.',
    decimalSeparator: ',',
    symbolSeparator: ' ',
  );

  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,935 €

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

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

  String jpySymbol = CurrencyFormat.jpy.symbol; // ¥
  String usdSymbol = CurrencyFormat.fromCode('usd')!.symbol; // $

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

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

  CurrencyFormat noSpaceSettings =
      CurrencyFormat.usd.copyWith(symbolSeparator: '');
  String noSpace =
      CurrencyFormatter.format(amount, noSpaceSettings); // $1,910.93

  CurrencyFormat negativeBeforeSymbolSettings = CurrencyFormat.tryx.copyWith(
    negativeSignPlacement: NegativeSignPlacement.beforeSymbol,
    symbolSeparator: '',
  );
  String negativeBeforeSymbol = CurrencyFormatter.format(
      amount, negativeBeforeSymbolSettings); // -₺1,910.93

  int intAmount = 3;
  String noDecimal = CurrencyFormatter.format(intAmount, euroSettings); // 3 €

  String enforceDecimal = CurrencyFormatter.format(
    intAmount,
    euroSettings,
    enforceDecimals: true,
  ); // 3,00 €

  CurrencyFormat noSymbolFormat =
      euroSettings.copyWith(symbol: '', symbolSeparator: '');
  String noSymbol =
      CurrencyFormatter.format(amount, noSymbolFormat); // 1.910,93

  // --------------------------------------------------------------------------
  // Custom currency list

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

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

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

  CurrencyFormat? localCurrency() =>
      CurrencyFormat.fromSymbol(CurrencyFormat.localSymbol, myCurrencies);

  // --------------------------------------------------------------------------

  print('Formatted: $formatted');
  print('Compact: $compact');
  print('Three Decimal: $threeDecimal');
  print('Parsed Formatted: $parseFormatted');
  print('Parsed Compact: $parseCompact');
  print('Parsed Three Decimal: $parseThreeDecimal');
  print('In USD: $inUSD');
  print('In RUB: $inRUB');
  print('JPY Symbol: $jpySymbol');
  print('USD Symbol: $usdSymbol');
  print('In System Currency: $inSystemCurrency');
  print('From Symbol (£): $fromSymbol');
  print('No Space USD: $noSpace');
  print('Negative Before Symbol: $negativeBeforeSymbol');
  print('No Decimal (int amount): $noDecimal');
  print('Enforce Decimal: $enforceDecimal');
  print('No Symbol: $noSymbol');
  print('Local Currency: ${localCurrency()}');
}
47
likes
160
points
8.6k
downloads

Publisher

verified publisherroman910.tk

Weekly Downloads

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.

Repository (GitHub)

Documentation

API reference

License

Unlicense (license)

Dependencies

intl, web

More

Packages that depend on currency_formatter