Kart Zhai
Kart Zhai

Reputation: 31

Flutter Get Number Value from Currency format

currently my textfield display in currency format example IDR 120.000

and i want to get the value as 120000, how can i get that value easily?

Upvotes: 0

Views: 590

Answers (2)

vijay saini
vijay saini

Reputation: 441

You can get the numbers by using this

final regDigit = RegExp(r'\d+');

final strValue = 'IDR 120.000';

print(strValue.replaceAll(regDigit, '')); // output => 120000

An Extension to format numbers into Indian currency format:

final regDigit = RegExp(r'\d+');

extension digitExtractor on String {
  
  String get extractString {
    //return this.replaceAll(regDigit, '');
    return regDigit.allMatches(this).map((match) => match.group(0)).join();
  }
  
  int get extractInt {
    // parse or default 0
    return int.tryParse(extractString) ?? 0;
  }
}

Now you can get value easily by using it like this:

final strValue = 'IDR 120.000';

print(strValue.extractString); // to get value in string

print(strValue.extractInt); // to get value in int

Upvotes: 0

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657168

I would split at the space and parse the remaining part:

var numValue = double.tryParse(currValue.split(' ')[1])

Upvotes: 1

Related Questions