Home > Enterprise >  How to add space between numbers?
How to add space between numbers?

Time:03-05

Tell me how to split the number of spaces that appear between the numbers (1 000, 1 000 000, etc.). I understandis that this should happen in a row and reverse it. then break the glue and turn over again. I just managed to expand. I can't figure out how to break it down. Or maybe there are some methods in the dart format like in others? In internet there are a lot of decisions for javascript or other languages wich have Format methods. But i didn't find something for Dart.

  void main(List<String> args) {
  var num = 123456789;

  var num1 = (num.toString()).split('').reversed.join();


  print(num1);
}

CodePudding user response:

For formatting and localisation of currencies or quotes, prices and digits checkout intl package and NumberFormat class, because I assume this is what you want to achieve. Different countries have different approaches for displaying numbers.

Also here you have nice solution that works.

CodePudding user response:

You should use regex to find three-digits and separate by a single space(' '). Here is an extension function (plus a working sample) to do that on any string containing numbers:

extension StringNumberExtension on String {
  String spaceSeparateNumbers() {
    final result = this.replaceAllMapped(
        RegExp(r'(\d{1,3})(?=(\d{3}) (?!\d))'), (Match m) => '${m[1]} ');
    return result;
  }
}

void main() async {
  print(
      'this is a number: 12345678901234567890123456789'.spaceSeparateNumbers());
}
  •  Tags:  
  • dart
  • Related