4

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

1
  • @F.S07 can you try with my answer? Commented Aug 24, 2021 at 11:27

2 Answers 2

13

Try with this

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

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

output: 1,000,000.00

Sign up to request clarification or add additional context in comments.

2 Comments

-wrong comment-
it rounds double to int which is not usable for crypto price
2

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]},") }");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.