-
Notifications
You must be signed in to change notification settings - Fork 2
/
price_format.dart
38 lines (36 loc) · 1.06 KB
/
price_format.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
extension PriceFormatExt on double {
/// Return the value formatted as would be a price with spaces.
///
/// If the [currency] parameter is used, it will be used as the currency
/// symbol.
///
/// ## Example
///
/// ```dart
/// 3700440.0.toPriceValue(); // 3 700 440,00
/// 3700440.0.toPriceValue(currency: '€'); // 3 700 440,00 €
/// ```
String toPriceValue({String? currency}) {
final base = toStringAsFixed(2);
final parts = base.split('.');
final intPart = parts[0];
final decimalPart = parts[1];
final buffer = StringBuffer();
final intPartContent = <String>[];
final reversedIntPart = intPart.split('').reversed.toList();
for (int i = 0; i < reversedIntPart.length; i++) {
final char = reversedIntPart[i];
if (i % 3 == 0) {
intPartContent.add(' ');
}
intPartContent.add(char);
}
buffer
..write(intPartContent.reversed.join().trim())
..write(',$decimalPart');
if (currency != null) {
buffer.write(' $currency');
}
return buffer.toString();
}
}