44 lines
1.5 KiB
Dart
44 lines
1.5 KiB
Dart
import 'package:intl/intl.dart';
|
|
|
|
/// Formatting utility helpers for currency, percentages, dates, and numbers.
|
|
class Formatters {
|
|
static final NumberFormat _currencyFormat = NumberFormat.currency(symbol: '\$', decimalDigits: 2);
|
|
static final NumberFormat _percentFormat = NumberFormat.decimalPercentPattern(decimalDigits: 2);
|
|
|
|
/// Formats currency values (e.g., $1,234.56).
|
|
static String formatCurrency(double? value, {String symbol = '\$'}) {
|
|
if (value == null) return '-';
|
|
if (symbol == '\$') return _currencyFormat.format(value);
|
|
return NumberFormat.currency(symbol: symbol, decimalDigits: 2).format(value);
|
|
}
|
|
|
|
/// Formats percentage value with sign (e.g. +3.45%).
|
|
static String formatPercent(double? value) {
|
|
if (value == null) return '0.00%';
|
|
final formatted = _percentFormat.format(value / 100);
|
|
return value >= 0 ? '+$formatted' : formatted;
|
|
}
|
|
|
|
/// Formats compact numbers (e.g. 1.2M, 3.4B).
|
|
static String formatCompactNumber(double? value) {
|
|
if (value == null) return '-';
|
|
return NumberFormat.compact().format(value);
|
|
}
|
|
|
|
/// Formats ISO date time string to readable short date.
|
|
static String formatDate(String? isoDate) {
|
|
if (isoDate == null || isoDate.isEmpty) return '-';
|
|
try {
|
|
final dt = DateTime.parse(isoDate).toLocal();
|
|
return DateFormat('dd.MM.yyyy HH:mm').format(dt);
|
|
} catch (_) {
|
|
return isoDate;
|
|
}
|
|
}
|
|
|
|
/// Formats date to day-month format.
|
|
static String formatShortDate(DateTime dt) {
|
|
return DateFormat('dd. MMM yyyy').format(dt);
|
|
}
|
|
}
|