Skip to content

Commit 7fe6de7

Browse files
committed
v4.1.6
1 parent c0ab8e0 commit 7fe6de7

File tree

10 files changed

+401
-99
lines changed

10 files changed

+401
-99
lines changed

.idea/libraries/Dart_Packages.xml

Lines changed: 115 additions & 95 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/Flutter_Plugins.xml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export 'src/utils/compact_format_type.dart';
2+
export 'src/utils/money_formatter_settings.dart';
3+
export 'src/utils/money_formatter_output.dart';
4+
export 'src/utils/money_formatter_compare.dart';
5+
export 'src/flutter_money_formatter_base.dart';
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import 'package:intl/intl.dart';
2+
3+
import '/money_formatter/src/utils/compact_format_type.dart';
4+
import '/money_formatter/src/utils/money_formatter_settings.dart';
5+
import '/money_formatter/src/utils/money_formatter_output.dart';
6+
import '/money_formatter/src/utils/money_formatter_compare.dart';
7+
8+
part 'utils/utilities.dart';
9+
10+
/// [FlutterMoneyFormatter] instance
11+
class MoneyFormatter {
12+
late _Utilities _utilities;
13+
14+
/// Init instance of [FlutterMoneyFormatter]
15+
///
16+
/// [amount] (@required) the number that will be formatted
17+
MoneyFormatter({required this.amount, this.settings}) {
18+
settings = settings ?? MoneyFormatterSettings();
19+
output = _getOutput();
20+
comparator = MoneyFormatterCompare(amount: amount);
21+
}
22+
23+
/// Amount number that will be formatted.
24+
double amount;
25+
26+
/// The formatter settings
27+
MoneyFormatterSettings? settings;
28+
29+
/// Returns compiled and formatted output in several formats.
30+
late MoneyFormatterOutput output;
31+
32+
/// Comparator
33+
late MoneyFormatterCompare comparator;
34+
35+
/// output builder
36+
MoneyFormatterOutput _getOutput() {
37+
_utilities = _Utilities(amount: amount, settings: settings);
38+
39+
String urs = _utilities.refineSeparator;
40+
int decSepCharPos = urs.indexOf(settings?.decimalSeparator ?? '˘');
41+
42+
return MoneyFormatterOutput(
43+
nonSymbol: urs,
44+
symbolOnLeft: '${settings?.symbol ?? ''}${_utilities.spacer}$urs',
45+
symbolOnRight: '$urs${_utilities.spacer}${settings?.symbol ?? ''}',
46+
compactNonSymbol: _compactNonSymbol,
47+
compactSymbolOnLeft:
48+
'${settings?.symbol ?? ''}${_utilities.spacer}$_compactNonSymbol',
49+
compactSymbolOnRight:
50+
'$_compactNonSymbol${_utilities.spacer}${settings?.symbol ?? ''}',
51+
fractionDigitsOnly:
52+
urs.substring((-1 == decSepCharPos ? 0 : decSepCharPos + 1)),
53+
withoutFractionDigits: urs.substring(
54+
0, -1 == decSepCharPos ? urs.length - 1 : decSepCharPos));
55+
}
56+
57+
/// returns FlutterMoneyFormatter after calculating amount.
58+
MoneyFormatter fastCalc(
59+
{required FastCalcType type, required double amount}) {
60+
switch (type) {
61+
case FastCalcType.addition:
62+
this.amount += amount;
63+
break;
64+
65+
case FastCalcType.substraction:
66+
this.amount -= amount;
67+
break;
68+
69+
case FastCalcType.multiplication:
70+
this.amount *= amount;
71+
break;
72+
73+
case FastCalcType.division:
74+
this.amount /= amount;
75+
break;
76+
77+
case FastCalcType.percentageAddition:
78+
this.amount += (amount / 100) * this.amount;
79+
break;
80+
81+
case FastCalcType.percentageSubstraction:
82+
this.amount -= (amount / 100) * this.amount;
83+
break;
84+
}
85+
86+
return this;
87+
}
88+
89+
/// Copies current instance and change some values to the new instance.
90+
MoneyFormatter copyWith(
91+
{double? amount,
92+
String? symbol,
93+
String? thousandSeparator,
94+
String? decimalSeparator,
95+
int? fractionDigits,
96+
String? symbolAndNumberSeparator,
97+
CompactFormatType? compactFormatType}) {
98+
MoneyFormatterSettings? ts = settings;
99+
100+
MoneyFormatterSettings mfs = MoneyFormatterSettings(
101+
symbol: symbol ?? ts?.symbol,
102+
thousandSeparator: thousandSeparator ?? ts?.thousandSeparator,
103+
decimalSeparator: decimalSeparator ?? ts?.decimalSeparator,
104+
symbolAndNumberSeparator:
105+
symbolAndNumberSeparator ?? ts?.symbolAndNumberSeparator,
106+
fractionDigits: fractionDigits ?? ts?.fractionDigits,
107+
compactFormatType: compactFormatType ?? ts?.compactFormatType);
108+
109+
return MoneyFormatter(amount: amount ?? this.amount, settings: mfs);
110+
}
111+
112+
/// Returns compact format number without currency symbol
113+
String get _compactNonSymbol {
114+
String compacted = _utilities.baseCompact.format(amount);
115+
String numerics = RegExp(r'(\d+\.\d+)|(\d+)')
116+
.allMatches(compacted)
117+
// ignore: no_wildcard_variable_uses
118+
.map((_) => _.group(0))
119+
.toString()
120+
.replaceAll('(', '')
121+
.replaceAll(')', '');
122+
123+
String alphas = compacted.replaceAll(numerics, '');
124+
125+
String reformat = NumberFormat.currency(
126+
symbol: '',
127+
decimalDigits:
128+
!numerics.contains('.') ? 0 : settings?.fractionDigits)
129+
.format(num.parse(numerics));
130+
131+
return '$reformat$alphas';
132+
}
133+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// An enum to be used on compact text format
2+
enum CompactFormatType { short, long }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// [MoneyFormatterCompare] instance.
2+
///
3+
/// This instance is used to hold utilities in comparing values held by [FlutterMoneyFormatter]
4+
class MoneyFormatterCompare {
5+
MoneyFormatterCompare({required this.amount});
6+
7+
final double amount;
8+
9+
/// Check current instance amount is lower than [amount] or not
10+
bool isLowerThan(double amount) => this.amount < amount;
11+
12+
/// Check current instance amount is greater than [amount] or not
13+
bool isGreaterThan(double amount) => this.amount > amount;
14+
15+
/// Check current instance amount is equal than [amount] or not
16+
bool isEqual(double amount) => this.amount == amount;
17+
18+
/// Check current instance amount is equal or lower than [amount] or not
19+
bool isEqualOrLowerThan(double amount) => this.amount <= amount;
20+
21+
/// Check current instance amount is equal or greater than [amount] or not
22+
bool isEqualOrGreaterThan(double amount) => this.amount >= amount;
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class MoneyFormatterOutput {
2+
/// Init instance of [MoneyFormatterOutput]
3+
MoneyFormatterOutput(
4+
{required this.nonSymbol,
5+
required this.symbolOnLeft,
6+
required this.symbolOnRight,
7+
required this.compactNonSymbol,
8+
required this.compactSymbolOnLeft,
9+
required this.compactSymbolOnRight,
10+
required this.fractionDigitsOnly,
11+
required this.withoutFractionDigits});
12+
13+
/// Returns formatted number without currency symbol
14+
final String nonSymbol;
15+
16+
/// Returns formatted number with currency symbol on the left side.
17+
final String symbolOnLeft;
18+
19+
/// Returns formatted number with currency symbol on the right side.
20+
final String symbolOnRight;
21+
22+
/// Returns compact format number without currency symbol
23+
final String compactNonSymbol;
24+
25+
/// Returns compact format number with currency symbol on the left side.
26+
final String compactSymbolOnLeft;
27+
28+
/// Returns compact format number with currency symbol on the right side.
29+
final String compactSymbolOnRight;
30+
31+
/// Returns decimal-only with length as specified on fractionDigits.
32+
final String fractionDigitsOnly;
33+
34+
/// Returns formatted number without decimal.
35+
final String withoutFractionDigits;
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import '/money_formatter/src/utils/compact_format_type.dart';
2+
3+
/// [MoneyFormatterSettings] instance.
4+
///
5+
/// This instance is used as a configurator for the [FlutterMoneyFormatter] instance.
6+
/// You can change the output of [FlutterMoneyFormatter] through this instance.
7+
class MoneyFormatterSettings {
8+
/// Init instance of [MoneyFormatterSettings]
9+
MoneyFormatterSettings(
10+
{this.symbol,
11+
this.thousandSeparator,
12+
this.decimalSeparator,
13+
this.symbolAndNumberSeparator,
14+
this.fractionDigits,
15+
this.compactFormatType}) {
16+
symbol = symbol ?? '\$';
17+
thousandSeparator = thousandSeparator ?? ',';
18+
decimalSeparator = decimalSeparator ?? '.';
19+
symbolAndNumberSeparator = symbolAndNumberSeparator ?? ' ';
20+
fractionDigits = fractionDigits ?? 2;
21+
compactFormatType = compactFormatType ?? CompactFormatType.short;
22+
}
23+
24+
/// The [symbol] that will be used on formatted output, default value is $ (Dollar Sign)
25+
String? symbol;
26+
27+
// The character that will be used as thousand separator on formatted output, default value is ',' (comma)
28+
String? thousandSeparator;
29+
30+
/// The character that will be used as decimal separator on formatted output, default value is '.' (dot)
31+
String? decimalSeparator;
32+
33+
/// The character that will be used as separator between the numbers and the symbol.
34+
String? symbolAndNumberSeparator;
35+
36+
/// The fraction digits that will be used on formatted output, default value is 2.
37+
int? fractionDigits;
38+
39+
/// Compact format type, for example using 'million' or 'M'
40+
CompactFormatType? compactFormatType;
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
part of '../flutter_money_formatter_base.dart';
2+
3+
enum FastCalcType {
4+
addition,
5+
substraction,
6+
multiplication,
7+
division,
8+
percentageAddition,
9+
percentageSubstraction
10+
}
11+
12+
class _Utilities {
13+
_Utilities({required this.amount, this.settings}) {
14+
settings = settings ?? MoneyFormatterSettings();
15+
}
16+
17+
final double amount;
18+
19+
MoneyFormatterSettings? settings;
20+
21+
/// Returns formatted number
22+
String get baseFormat => NumberFormat.currency(
23+
symbol: '', decimalDigits: settings?.fractionDigits, locale: 'en_US')
24+
.format(amount);
25+
26+
/// Returns formatted number with refined separator chars
27+
String get refineSeparator => baseFormat
28+
.replaceAll(',', '(,)')
29+
.replaceAll('.', '(.)')
30+
.replaceAll('(,)', settings?.thousandSeparator ?? ' ')
31+
.replaceAll('(.)', settings?.decimalSeparator ?? ' ');
32+
33+
/// Returns spacer as `spaceBetweenSymbolAndNumber` value
34+
String get spacer => settings?.symbolAndNumberSeparator ?? ' ';
35+
36+
/// Returns base compact format
37+
NumberFormat get baseCompact =>
38+
settings?.compactFormatType == CompactFormatType.short
39+
? NumberFormat.compact()
40+
: NumberFormat.compactLong();
41+
}

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dependencies:
1313
dio: ^5.8.0+1
1414
device_meta: ^2.1.7
1515
encrypt: ^5.0.3
16+
intl: ^0.20.2
1617

1718
flutter:
1819
sdk: flutter

0 commit comments

Comments
 (0)