mx_formatter 1.0.0
mx_formatter: ^1.0.0 copied to clipboard
A Dart package for formatting quantities, percentages, and monetary values. Removes trailing zeros and adds thousand separators with ease.
example/main.dart
// ignore_for_file: avoid_print
import 'package:mx_formatter/mx_formatter.dart';
void main() {
_extensionExamples();
_staticApiExamples();
}
// ─── Extension API (recommended) ─────────────────────────────────────────────
//
// After importing mx_formatter, every num (int & double) gains three methods:
// .moneyFormat() .qtyFormat() .percentFormat()
void _extensionExamples() {
print('=== Extension API ===');
// ── Money ──────────────────────────────────────────────────────────────────
print('-- moneyFormat --');
final int balance = 102321121;
print(balance.moneyFormat()); // 102 321 121
final double price = 10321121.21;
print(price.moneyFormat()); // 10 321 121.21
print(1000.moneyFormat()); // 1 000
print(1000000.moneyFormat()); // 1 000 000
print(999.moneyFormat()); // 999
print((-1500.5).moneyFormat()); // -1 500.5
// Custom separators
print(1000000.moneyFormat(thousandSeparator: ',')); // 1,000,000
print(1500.5.moneyFormat(decimalSeparator: ',')); // 1 500,5
// European style (dot thousands, comma decimal)
print(
1234567.89.moneyFormat(
thousandSeparator: '.',
decimalSeparator: ',',
),
); // 1.234.567,89
print('');
// ── Quantity ───────────────────────────────────────────────────────────────
print('-- qtyFormat --');
print(10.12123123.qtyFormat()); // 10.12
print(10.0.qtyFormat()); // 10
print(10.10.qtyFormat()); // 10.1
print(0.0.qtyFormat()); // 0
print((-3.5).qtyFormat()); // -3.5
print(10.123456.qtyFormat(maxDecimalPlaces: 4)); // 10.1235
// Works on int too
print(42.qtyFormat()); // 42
print('');
// ── Percentage ─────────────────────────────────────────────────────────────
print('-- percentFormat --');
print(10.12123123.percentFormat()); // 10.12%
print(10.0.percentFormat()); // 10%
print(0.0.percentFormat()); // 0%
print((-5.5).percentFormat()); // -5.5%
// Works on int
print(50.percentFormat()); // 50%
print('');
}
// ─── Static API (classic style) ───────────────────────────────────────────────
//
// The static classes are still fully available if you prefer explicit syntax.
void _staticApiExamples() {
print('=== Static API ===');
print(QuantityFormatter.format(10.12123123)); // 10.12
print(QuantityFormatter.formatPercent(10.0)); // 10%
print(MoneyFormatter.format(102321121)); // 102 321 121
}