flutter extension
fluttermoneyformatter is a flutter extension to formatting various types of currencies according to the characteristics you like, without having to be tied to any localization.
install of flutter extension
for complete steps in installing fluttermoneyformatter
you can see in the installation guide.
usage
flutter extension
import the library
import 'package:flutter_money_formatter/flutter_money_formatter.dart';
getting started
flutter extension
to be able to format your double
value into the various formats you want, you first need to create a fluttermoneyformatter
instance like the following:
fluttermoneyformatter fmf = fluttermoneyformatter(
amount: 12345678.9012345
);
note, the code above still uses the default configuration as explained here.
after that, you can request various results of the format as follows:
// normal form
print(fmf.output.nonsymbol); // 12,345,678.90
print(fmf.output.symbolonleft); // $ 12,345,678.90
print(fmf.output.symbolonright); // 12,345,678.90 $
print(fmf.output.fractiondigitsonly); // 90
print(fmf.output.withoutfractiondigits); // 12,345,678
// compact form
print(fmf.output.compactnonsymbol) // 12.3m
print(fmf.output.compactsymbolonleft) // $ 12.3m
print(fmf.output.compactsymbolonright) // 12.3m $
flutter extension
if you will use the output format several times, i strongly recommend that you initialize a variable as in the following example:
moneyformatteroutput fo = fmf.output;
or directly when initializing the fluttermoneyformatter
instance as in the following example:
moneyformatteroutput fo = fluttermoneyformatter(
amount: 12345678.9012345
).output;
flutter extension
so you can immediately take the value more easily as in the following example:
// normal form
print(fo.nonsymbol); // 12,345,678.90
print(fo.symbolonleft); // $ 12,345,678.90
print(fo.symbolonright); // 12,345,678.90 $
print(fo.fractiondigitsonly); // 90
print(fo.withoutfractiondigits); // 12,345,678
// compact form
print(fo.compactnonsymbol) // 12.3m
print(fo.compactleftsymbol) // $ 12.3m
print(fo.compactrightsymbol) // 12.3m $
see demo section to get more info.
configurations
flutter extension
to adjust the format to suit your needs, you can set it through the settings
parameter:
fluttermoneyformatter fmf = new fluttermoneyformatter(
amount: 12345678.9012345,
settings: moneyformattersettings(
symbol: 'idr',
thousandseparator: '.',
decimalseparator: ',',
symbolandnumberseparator: ' ',
fractiondigits: 3,
compactformattype: compactformattype.sort
)
)
flutter extension
of course you are not required to initialize the entire configuration in the settings
(moneyformattersettings
) parameter as in the example above. you can change one or more of the configurations above. this is because each configuration above is not mandatory and has a default value.
properties, methods, and functions
-
fluttermoneyformatter
property names | data type | descriptions |
---|---|---|
amount |
double |
amount number that will be formatted. |
settings |
moneyformattersettings |
see here. |
output |
moneyformatteroutput |
see here. |
comparator |
moneyformattercompare |
see here. |
copywith |
fluttermoneyformatter |
see here |
fastcalc |
fluttermoneyformatter |
see here |
-
moneyformattersettings
configuration property | data type | default value | description |
---|---|---|---|
symbol |
string |
$ (dollar sign) |
the symbol that will be used on formatted output. |
thousandseparator |
string |
, |
the character that will be used as thousand separator on formatted output. |
decimalseparator |
string |
. |
the character that will be used as decimal separator on formatted output. |
fractiondigits |
int |
2 |
the fraction digits that will be used on formatted output. |
symbolandnumberseparator |
string |
' ' (space) |
the character that will be used as separator between formatted number and currency symbol. |
compactformattype |
compactformattype |
compactformattype.short |
see here. |
-
compactformattype
flutter extension
you can change the type of compact format like for million using m
or million
, or trillion using t
or trillion
. and so on. this type only supports two type as described below:
value | description |
---|---|
compactformattype.short |
used to make the compact format displayed using short text. |
compactformattype.long |
used to make the compact format displayed using long text. |
-
moneyformatteroutput
you can use formats that match your needs through properties found in the moneyformatteroutput
instance.
property names | data type | descriptions |
---|---|---|
nonsymbol |
string |
the results of the format of the currency are normal and without a currency symbol. example: 12,345,678.90 |
symbolonleft |
string |
the results of the normal currency format and with currency symbols are on the left. example: $ 12,345,678.90 |
symbolonright |
string |
the results of the normal currency format and with currency symbols are on the right. example: 12,345,678.90 $ |
compactnonsymbol |
string |
the results of the currency format are compact and without a currency symbol. example: 12.3m |
compactsymbolonleft |
string |
the results of the currency format are compact and with currency symbols on the left. example: $ 12.3m |
compactsymbolonright |
string |
the results of the currency format are compact and with currency symbols on the right. example: 12.3m $ |
fractiondigitsonly |
string |
only give the fraction value. example: 90 |
withoutfractiondigits |
string |
give a value without fractions. example: 12,345,678 |
-
moneyformattercompare
method | parameter | descriptions |
---|---|---|
islowerthan |
amount |
check current instance-amount is lower than [amount] or not. |
isgreaterthan |
amount |
check current instance-amount is greater than [amount] or not. |
isequal |
amount |
check current instance amount is equal than [amount] or not. |
isequalorlowerthan |
amount |
check current instance amount is equal or lower than [amount] or not. |
isequalorgreaterthan |
amount |
check current instance amount is equal or greater than [amount] or not. |
example of using a comparator:
fluttermoneyformatter fmf = fluttermoneyformatter(amount: 12345678.9012345);
double comparervalue = 5678.9012;
print(fmf.comparator.isequal(comparervalue)); // false
print(fmf.comparator.isgreaterthan(comparervalue)); // true
fastcalc
fastcalc
is a function that can be used to perform various fast calculation processes that you might need. in implementing it, the fastcalc
function gives the output of afluttermoneyformatter
instance so you can perform several calculation functions at once with the chaining method.
function:
fluttermoneyformatter fastcalc({
@required fastcalctype type,
@required double amount
})
implementation:
fluttermoneyformatter fmf = fluttermoneyformatter(amount: 12345.678);
fmf.fastcalc(type: fastcalctype.addition, amount: 1.111);
fmf.fastcalc(type: fastcalctype.substraction, amount: 2.222);
print(fmf.output.nonsymbol); // 12,345.68
because it supports the chaining process, the example above can be shortened as follows:
fluttermoneyformatter fmf = fluttermoneyformatter(amount: 12345.678)
.fastcalc(type: fastcalctype.addition, amount: 1.111)
.fastcalc(type: fastcalctype.substraction, amount: 2.222);
print(fmf.output.nonsymbol); // 12,345.68
the type
parameter used by the fastcalc
function has the fastcalctype
data type which is an enum. the following table is an explanation for the fastcalctype
enum:
index | name | description |
---|---|---|
0 | addition | used to do addition calculations. |
1 | substraction | used to do substraction calculations. |
2 | multiplication | used to do multiplication calculations. |
3 | division | used to do division calculations. |
4 | percentageaddition | used to do the addition calculations base on percentage. |
5 | percentagesubstraction | used to do the substraction calculations base on percentage. |
duplicating instance
for some reasons, you may need to duplicate the instance
and just need to change some configurations. to do that, you can use the copywith
method as below:
fluttermoneyformatter fmf = fluttermoneyformatter(amount: 12345678.9012345);
print(fmf.output.symbolonleft); // $ 12,345,678.90
print(fmf.copywith(symbol: 'idr', symbolandnumberseparator: '-').output.symbolonleft); // idr-12,345,678.90
Comments are closed.