nagendra nag
nagendra nag

Reputation: 1332

convert number to indian currency format in flutter

I want to convert the number to Indian currency as mentioned in the below format

1 - 1

10 - 10

100 - 100

1000 - 1,000 or 1k

10000 - 10,000 or 10k

100000 - 1,00,000 or 100k or 1L

1000000 - 10,00,000 or 10L

...

In flutter, we have NumberFormat.currency(locale: 'en_IN');. but this is not covering all the cases I have mentioned above is there any possibility of achieving my requirement.

I have searched on the internet but I didn't get any resources which satisfy my requirement.

Upvotes: 0

Views: 983

Answers (2)

nagendra nag
nagendra nag

Reputation: 1332

with help of @Er1's comment i have covered all cases mentioned in the question.

String getIndianCurrencyInShorthand({required double amount}) {
  final inrShortCutFormatInstance =
      NumberFormat.compactSimpleCurrency(locale: 'en_IN', name: "");
  var inrShortCutFormat = inrShortCutFormatInstance.format(amount);
  if (inrShortCutFormat.contains('T')) {
    return inrShortCutFormat.replaceAll(RegExp(r'T'), 'k');
  }
  return inrShortCutFormat;
} 


getIndianCurrencyInShorthand(1000);

Upvotes: 1

Yuvraj Desai
Yuvraj Desai

Reputation: 285

To convert 2845000 to 28,45,000 you can use following method :

class MoneyFormat {
String price;

String moneyFormat(String price) {
if (price.length > 2) {
  var value = price;
  value = value.replaceAll(RegExp(r'\D'), '');
  value = value.replaceAll(RegExp(r'\B(?=(\d{3})+(?!\d))'), ',');
  return value;
  }
 }
}

you need to call function with :

var result=MoneyFormat().moneyFormat('2845000');
print(result);
//----this will return : 28,45,000

To convert 1000 to 1K you can use following method :

function convertMoney(value)
{
    if(value>=1000000)
    {
        value=(value/1000000)+"M"
    }
    else if(value>=1000)
    {
        value=(value/1000)+"K";
    }
    return value;
}
print(convertMoney(1000));
print(convertMoney(10000));
print(convertMoney(300000));
print(convertMoney(3000000));

Output will be:

1K
10K
300K
3M

Upvotes: 1

Related Questions