Need to show prices nicely in your Flutter app? The intl
package makes it super easy!
Get Started
First, make sure you add the intl
package to your pubspec.yaml
file and import it:
import 'package:intl/intl.dart';
Common Formatting Methods
Here are some common ways to format numbers as currency:
Specific Locale and Symbol
NumberFormat.currency(locale: 'en_US', symbol: '\$').format(12.2);
// Result: $12.2
Default Locale Formatting
If you don't specify a locale, it uses the device's settings.
NumberFormat.currency().format(12.2);
// Result could be US 12.2 or EUR 12.2 depending on the device locale
Without Decimal Digits
Just want a whole number price?
NumberFormat.currency(decimalDigits: 0).format(12.2);
// Result: US 12 (symbol may vary by locale)
Simple Currency Format
NumberFormat.simpleCurrency().format(12.2);
// Result: $12.2
Compact Format for Large Numbers
Great for showing large values in a shorter way.
NumberFormat.compactSimpleCurrency().format(12000000);
// Result: $1.2M
Using these simple methods from the intl
package, you can display prices in many different formats easily!