How to Add Indonesian Currency (Rupiah/IDR) Format in Flutter
Are you building a Flutter app and need to show prices in Indonesian Rupiah? Let me show you an easy way to do this!
Step 1: Add the intl Package
First, we need to add the intl package. This package helps us format numbers into currency. Add this line to your pubspec.yaml file:
Then run flutter pub get in your terminal.
Step 2: Create a Currency Format Helper
Create a new file called currency_format.dart. You can put this file in your utils folder or wherever you keep helper functions.
Add this code to your file:
import 'package:intl/intl.dart';
class CurrencyFormat {
static String convertToIdr(double number, int decimalDigit) {
NumberFormat currencyFormatter = NumberFormat.currency(
locale: 'id',
symbol: 'Rp ',
decimalDigits: decimalDigit,
);
return currencyFormatter.format(number);
}
}
Let’s understand what this code does:
- locale: id => tells the formatter to use Indonesian number format
- symbol: Rp => adds the Rupiah symbol before the number
- decimalDigits: lets you choose how many decimal places you want to show
Step 3: Using the Currency Format
Now you can easily format any number into Rupiah. Here’s how:
// Example: Convert 50000 to Rupiah
double money = 50000;
Text(CurrencyFormat.convertToIdr(money, 0))
// This will show: Rp 50.000
If you set decimalDigit to 2, it will show: Rp 50.000,00
That’s all! Now you can show Indonesian currency format in your Flutter app.
Tips:
- Use decimalDigit: 0 for whole numbers
- Use decimalDigit: 2 if you need to show cents
- Remember to import your currency_format.dart file where you want to use it