{

How to Format a currency Dart or Flutter | Dart By Example


In Dart, You have a class NumberFormat that formats numbers with currency formats of a different locale.

For example, if a given number is 100000, Some countries allow you to format a number as 100,000 and 100,000.00.

Dart NumberFormat example

Numberformat is a class in the intl package.

First, define dependency in pubspec.yaml

dev_dependencies:
    intl: any

Next, Install dependency using the dart pub get command in the terminal or It automatically downloads in the Visual studio terminal

intl.dart package provides NumberFormat class that formats the number as per Locale.

First, create an object of NumberFormat.

NumberFormat NumberFormat([String? newPattern, String? locale])

newpattern is a format of the number and Locale is a valid locale string. NumberFormat.format() method formats number as per pattern.

How to format a number with group separator for Currency in Dart?

In this section, You will format the currency number as per Locale. NumberFormat object provided with a pattern of commas and decimal format.

here is an example program

import 'package:intl/intl.dart';

final numberFormat = new NumberFormat("##,##0.00", "en_US");

void main() {
  print(numberFormat.format(1000000.10));
}

How to format numbers with 5 decimals length

Here numberformat is provided with 5 zeroes and local here is an example program

import 'package:intl/intl.dart';

final numberFormat = new NumberFormat("#####.000000", "en_US");

void main() {
  print(numberFormat.format(1000000.10));
}

Output:

1000000.100000

How to align currency to number in dart

NumberFormat.currency() method takes locale and Symbol string and creates instance.

Call format() method with input and format number with align currency symbol based on current locale.

import 'package:intl/intl.dart';

final numberFormat = NumberFormat.currency(locale: 'en_US', symbol: 'USD');

void main() {
  print(numberFormat.format(3421231));
}

Output:

USD3,421,231.00
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example