Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter/Dart - Format double to String with thousand separators AND digits after decimal point

I am trying to achieve a formatted String from a double variable, which will include thousand separators (",") AND will include always 2 digits after the decimal point (even if it's zero).

I've succeeded to achieve both of them separately, but couldn't achieve them combined together.

Example for what I'm looking to achieve:

100 => "100.00"
100.5 => "100.50"
1000 = "1,000.00"
1000.5 = > "1,000.50"
1000000 => "1,000,000.00"
1000000.53 => 1,000,000.53
etc...

I've tried to achieve this using:

NumberFormat.decimalPattern('en').format(
    double.parse(
        myDoubleVar.toStringAsFixed(2),
    ),
);

But it doesn't give my the decimal points if they are zero.

Does anyone know how can I achieve this?

Thank you

like image 476
F.SO7 Avatar asked Oct 21 '25 04:10

F.SO7


2 Answers

Try with this

double amount = 1000000;
NumberFormat numberFormat = NumberFormat("#,##0.00", "en_US");

print('${(numberFormat.format(amount))}');

output: 1,000,000.00

like image 120
Jahidul Islam Avatar answered Oct 22 '25 17:10

Jahidul Islam


Here I have used like this.

void main() {
  
  num balance = 120000;
    print("\$${balance.toStringAsFixed(2).replaceAllMapped(RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'), (Match m) => "${m[1]},") }");
}
like image 29
Rohit Chaurasiya Avatar answered Oct 22 '25 19:10

Rohit Chaurasiya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!