Home > Software design >  How to format money as millions in flutter?
How to format money as millions in flutter?

Time:01-09

I have formatted 500000 to 500k using Number formatter like this

NumberFormat.compact().format(int.parse(player.price!))

but i want to convert the number to half million like this 0.5m

is it possible ?

CodePudding user response:

  • The official method provided by Flutter is difficult to do this, so I wrote this
      test('1515'); //0.002M
      test('1215'); //0.001M
      test('151555'); // 0.152M
      test('1511445'); // 0.151M
      test('15114455'); // 15.1M

  String test(String str) {
    var result = NumberFormat.compact(locale: 'en').format(int.parse(str));
    if (result.contains('K') && result.length > 3) {
      result = result.substring(0, result.length - 1);
      var prefix = (result.split('.').last.length)   1;
      var temp = (double.parse(result) * .001).toStringAsFixed(prefix);
      result = double.parse(temp).toString()   'M';
    }
    return result;
  }

CodePudding user response:

Just divide by 100000

  void main() {
      double num = 500000;
      print(num/1000000);
    }

The output is

    0.5

CodePudding user response:

this function below returns you the result you want:

void main(){
    print(convertNumber(500000));
}
String convertNumber(int number) {
  
  return '${number/1000000} m';
}

output:

0.5 m

as you know if you want to have another formats you have to customize it. I think you are iranian. these codes at below can be useful for you to change any numbers to text:

void main(){
    print(convertNumberToWord(500000));
}
String convertNumberToWord(int number) {
  var result = <String>[];

  while (number > 0) {
    result.add(_numToWord(number % base));
    number = (number / base).floor();
  }
  if (result.length > 4) return '';

  for (var i = 0; i < result.length; i  ) {
    if (result[i] != '' && i != 0) result[i]  = ' ${scale[i]} و ';
  }
  result = result.reversed.toList();

  var words = result.join('');

  if (words.endsWith(endsWithAnd)) {
    words = words.substring(0, (words.length - 3));
  }

  words = trim(words);

  return words;
}

String _numToWord(int number) {
  var unit = 100;
  var result = '';

  while (unit > 0) {
    if ((number / unit).floor() * unit != 0) {
      if (numberToWord.containsKey(number)) {
        result  = numberToWord[number]!;
        break;
      } else {
        result  = '${numberToWord[(number / unit).floor() * unit]} و ';
        number %= unit;
      }
    }
    unit = (unit / 10).floor();
  }
  return result;
}
const scale = ['', 'هزار', 'میلیون', 'میلیارد'];
const base = 1000;
const endsWithAnd = ' و ';
const numberToWord = {
  0: '',
  1: 'یک',
  2: 'دو',
  3: 'سه',
  4: 'چهار',
  5: 'پنج',
  6: 'شش',
  7: 'هفت',
  8: 'هشت',
  9: 'نه',
  10: 'ده',
  11: 'یازده',
  12: 'دوازده',
  13: 'سیزده',
  14: 'چهارده',
  15: 'پانزده',
  16: 'شانزده',
  17: 'هفده',
  18: 'هجده',
  19: 'نوزده',
  20: 'بیست',
  30: 'سی',
  40: 'چهل',
  50: 'پنجاه',
  60: 'شصت',
  70: 'هفتاد',
  80: 'هشتاد',
  90: 'نود',
  100: 'صد',
  200: 'دویست',
  300: 'سیصد',
  400: 'چهارصد',
  500: 'پانصد',
  600: 'ششصد',
  700: 'هفتصد',
  800: 'هشتصد',
  900: 'نهصد',
};
String trim(String string) => string.replaceAll(whiteSpaceRegExp, '');
RegExp whiteSpaceRegExp = new RegExp(r'^\s |\s $');


output:

پانصد هزار

happy coding...

CodePudding user response:

String generate(double number) {
  return "${(number / 1000000).toStringAsFixed(2)} M";
}

double generatedNumber = 50000;
print(generate(generatedNumber));

CodePudding user response:

String numberFormat(int n) {
  String num = n.toString();
  int len = num.length;

  if (n >= 1000 && n < 1000000) {
    return num.substring(0, len - 3)   '.'   num.substring(len - 3, 1   (len - 3))   'k';
  } else if (n >= 1000000 && n < 1000000000) {
    return num.substring(0, len - 6)   '.'   num.substring(len - 6, 1   (len - 6))   'm';
  } else if (n > 1000000000) {
    return num.substring(0, len - 9)   '.'   num.substring(len - 9, 1   (len - 9))   'b';
  } else {
    return num.toString();
  }
}
  • Related