Aditya Patil
Aditya Patil

Reputation: 1486

Number formatter in flutter for Indian number system

How to write the below numbers into words like below sample in Flutter/dart.

there should be only 2 digits after decimal.

Upvotes: 0

Views: 91

Answers (2)

Sapto Sutardi
Sapto Sutardi

Reputation: 456

You can follow:

  1. Import package: INTL

import 'package:intl/intl.dart';

  1. Create method
String formatNumber(int number) {
  var formatter = NumberFormat("##,##,##,##0.00", "en_IN");
  if (number >= 10000000) {
    return formatter.format(number / 10000000) + " cr";
  } else if (number >= 100000) {
    return formatter.format(number / 100000) + " lac";
  } else {
    return formatter.format(number);
  }
}
  1. Call the method:
  print(formatNumber(164023)); // 1.64 lac
  print(formatNumber(23412456)); // 2.34 cr

Have a nice day...

Upvotes: 0

Siddharth Makadiya
Siddharth Makadiya

Reputation: 733

String convertNumberToWords(int number) {
  if (number >= 10000000) {
    return "${(number/10000000).toStringAsFixed(2)} cr";
  } else if (number >= 100000) {
    return "${(number/100000).toStringAsFixed(2)} lac";
  }
  return number.toString();
}

Userstanding :

#convertNumberToWords function takes in an integer number and returns a string that represents the number in words. If the number is greater than or equal to 10 million, the function returns the number divided by 10 million with two decimal places, followed by the string "cr".

If the number is greater than or equal to 100,000, the function returns the number divided by 100,000 with two decimal places, followed by the string "lac". If the number is less than 100,000, the function returns the number as a string.

Upvotes: 1

Related Questions