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

Issue

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

Solution

Try with this

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

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

output:
1,000,000.00

Answered By – Jahidul Islam

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.