Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart how to add commas to a string number

I'm trying to adapt this: Insert commas into number string to work in dart, but no luck.

either one of these don't work:

print("1000200".replaceAllMapped(new RegExp(r'/(\d)(?=(\d{3})+$)'), (match m) => "${m},"));
print("1000300".replaceAll(new RegExp(r'/\d{1,3}(?=(\d{3})+(?!\d))/g'), (match m) => "$m,"));

Is there a simpler/working way to add commas to a string number?

like image 469
Alex L. Avatar asked Aug 11 '15 00:08

Alex L.


4 Answers

You just forgot get first digits into group. Use this short one:

'12345kWh'.replaceAllMapped(RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'), (Match m) => '${m[1]},')

Look at the readable version. In last part of expression I added checking to any not digit char including string end so you can use it with '12 Watt' too.

RegExp reg = RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))');
String Function(Match) mathFunc = (Match match) => '${match[1]},';

List<String> tests = [
  '0',
  '10',
  '123',
  '1230',
  '12300',
  '123040',
  '12k',
  '12 ',
];

for (String test in tests) {    
  String result = test.replaceAllMapped(reg, mathFunc);
  print('$test -> $result');
}

It works perfectly:

0 -> 0
10 -> 10
123 -> 123
1230 -> 1,230
12300 -> 12,300
123040 -> 123,040
12k -> 12k
12  -> 12 
like image 133
kelegorm Avatar answered Sep 29 '22 06:09

kelegorm


import 'package:intl/intl.dart';

var f = NumberFormat("###,###.0#", "en_US");
print(f.format(int.parse("1000300")));

prints 1,000,300.0 check dart's NumberFormat here

The format is specified as a pattern using a subset of the ICU formatting patterns.

  • 0 A single digit
  • # A single digit, omitted if the value is zero
  • . Decimal separator
  • - Minus sign
  • , Grouping separator
  • E Separates mantissa and expontent
  • + - Before an exponent, to say it should be prefixed with a plus sign.
  • % - In prefix or suffix, multiply by 100 and show as percentage
  • ‰ (\u2030) In prefix or suffix, multiply by 1000 and show as per mille
  • ¤ (\u00A4) Currency sign, replaced by currency name
  • ' Used to quote special characters
  • ; Used to separate the positive and negative patterns (if both present)
like image 33
Clyde Santos Avatar answered Oct 02 '22 06:10

Clyde Santos


Try the following regex: (\d{1,3})(?=(\d{3})+$)

This will provide two backreferences, and replacing your number using them like $1,$2, will add commas where they are supposed to be.

like image 37
Dungeonfire Avatar answered Sep 29 '22 06:09

Dungeonfire


Let's take the example amount 12000. now our expected amount should be 12,000.00

so, the solution is

double rawAmount = 12000;
String amount = rawAmount.toStringAsFixed(2).replaceAllMapped(RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'), (Match m) => '${m[1]},');

or if you don't want to add .00 then, we just need to use toString() instead of toStringAsFixed().

String amount = rawAmount.toString().replaceAllMapped(RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'), (Match m) => '${m[1]},');
like image 28
Rushi Dave Avatar answered Oct 02 '22 06:10

Rushi Dave