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.
MXFormatter #
A lightweight, zero-dependency pure-Dart package for formatting quantities, percentages, and monetary values.
- Rounds to a configurable number of decimal places.
- Strips trailing zeros (and the decimal point when unnecessary).
- Adds a configurable thousand separator (default: space
' '). - Works on all platforms — Android, iOS, Linux, macOS, Web, Windows.
Features #
| Input | Output | Formatter |
|---|---|---|
10.12123123 |
"10.12" |
QuantityFormatter.format |
10.0 |
"10" |
QuantityFormatter.format |
10.10 |
"10.1" |
QuantityFormatter.format |
10.12123123 |
"10.12%" |
QuantityFormatter.formatPercent |
10.0 |
"10%" |
QuantityFormatter.formatPercent |
102_321_121 |
"102 321 121" |
MoneyFormatter.format |
10_321_121.21 |
"10 321 121.21" |
MoneyFormatter.format |
-1_500.5 |
"-1 500.5" |
MoneyFormatter.format |
Getting started #
Add the package to your pubspec.yaml:
dependencies:
mx_formatter: ^1.0.0
Then run:
dart pub get
Usage #
Import the library:
import 'package:mx_formatter/mx_formatter.dart';
The package offers two equivalent styles — pick the one you prefer:
| Style | Example |
|---|---|
| Extension (fluent) | 102321121.moneyFormat() |
| Static class (explicit) | MoneyFormatter.format(102321121) |
Extension API (fluent style) #
Every num value (int and double) gains three extension methods:
// Money
102321121.moneyFormat(); // "102 321 121"
10321121.21.moneyFormat(); // "10 321 121.21"
1000000.moneyFormat(thousandSeparator: ','); // "1,000,000"
// Quantity
10.12123123.qtyFormat(); // "10.12"
10.0.qtyFormat(); // "10"
42.qtyFormat(); // "42" (int works too)
// Percentage
10.12123123.percentFormat(); // "10.12%"
10.0.percentFormat(); // "10%"
50.percentFormat(); // "50%" (int works too)
Static API #
Quantity formatting #
// Rounds to 2 decimal places by default and strips trailing zeros.
QuantityFormatter.format(10.12123123); // "10.12"
QuantityFormatter.format(10.0); // "10"
QuantityFormatter.format(10.10); // "10.1"
QuantityFormatter.format(0.0); // "0"
QuantityFormatter.format(-3.5); // "-3.5"
// Custom number of decimal places.
QuantityFormatter.format(10.123456, maxDecimalPlaces: 4); // "10.1235"
QuantityFormatter.format(10.123456, maxDecimalPlaces: 0); // "10"
Percentage formatting #
// Same as format(), but appends the '%' symbol.
QuantityFormatter.formatPercent(10.12123123); // "10.12%"
QuantityFormatter.formatPercent(10.0); // "10%"
QuantityFormatter.formatPercent(0.0); // "0%"
QuantityFormatter.formatPercent(-5.5); // "-5.5%"
Money formatting #
// Space is the default thousand separator.
MoneyFormatter.format(102321121); // "102 321 121"
MoneyFormatter.format(10321121.21); // "10 321 121.21"
MoneyFormatter.format(1000); // "1 000"
MoneyFormatter.format(1000000); // "1 000 000"
MoneyFormatter.format(999); // "999"
MoneyFormatter.format(0); // "0"
MoneyFormatter.format(1500.5); // "1 500.5"
MoneyFormatter.format(-1500.5); // "-1 500.5"
// Custom separators.
MoneyFormatter.format(1000000, thousandSeparator: ','); // "1,000,000"
MoneyFormatter.format(1500.5, decimalSeparator: ','); // "1 500,5"
// European style: dot thousands, comma decimal.
MoneyFormatter.format(
1234567.89,
thousandSeparator: '.',
decimalSeparator: ',',
); // "1.234.567,89"
API reference #
NumMXFormatterExtension (on num) #
Extension on num — automatically available on every int and double.
| Method | Signature | Description |
|---|---|---|
moneyFormat |
({int maxDecimalPlaces, String thousandSeparator, String decimalSeparator}) |
Formats as monetary string |
qtyFormat |
({int maxDecimalPlaces}) |
Formats as quantity string |
percentFormat |
({int maxDecimalPlaces}) |
Formats as percentage string |
All parameters are optional (same defaults as the static classes).
QuantityFormatter #
A utility class with only static methods (cannot be instantiated).
format
static String format(
double value, {
int maxDecimalPlaces = 2,
})
Rounds value to at most maxDecimalPlaces decimal digits and removes
trailing zeros. The decimal point itself is also removed when the fractional
part becomes zero.
| Parameter | Type | Default | Description |
|---|---|---|---|
value |
double |
— | The number to format |
maxDecimalPlaces |
int |
2 |
Max digits after the decimal point (must be ≥ 0) |
formatPercent
static String formatPercent(
double value, {
int maxDecimalPlaces = 2,
})
Equivalent to format() with a % character appended to the result.
MoneyFormatter #
A utility class with only static methods (cannot be instantiated).
format
static String format(
double value, {
int maxDecimalPlaces = 2,
String thousandSeparator = ' ',
String decimalSeparator = '.',
})
Formats value as a monetary string. Integer digits are grouped by
thousandSeparator every three digits from the right. The fractional part
is rounded to maxDecimalPlaces digits with trailing zeros stripped.
| Parameter | Type | Default | Description |
|---|---|---|---|
value |
double |
— | The number to format |
maxDecimalPlaces |
int |
2 |
Max digits after the decimal point (must be ≥ 0) |
thousandSeparator |
String |
' ' |
Inserted between each group of 3 integer digits |
decimalSeparator |
String |
'.' |
Separates the integer and fractional parts |
Additional information #
- Source code: github.com/your-username/mx_formatter
- Issue tracker: github.com/your-username/mx_formatter/issues
- License: MIT
Contributions are welcome! Please open an issue or submit a pull request.