2

I have a simple program below. The output is 64.5 it only shows one decimal value but I wanted it to display the last two decimal values.

Ex. Your change is 64.50 USD

How can I achieve this in dart/flutter?

void main() {

  double money = 80.00;
  double price = 15.50;

  double change = money - price;

  print('Your change is $change USD');
}
1
  • 2
    BTW, I would highly recommend that you do not store currency as double. You'd be much better off storing the number of cents (or whatever the smallest indivisible unit of currency is) as int and then printing it in a friendlier format at the end. If you use double, you'll find that $0.10 + $0.10 + $0.10 is not $0.30 due to floating point error. Commented Apr 25, 2020 at 2:04

2 Answers 2

5

Try This:

void main() {

  double money = 80.00;
  double price = 15.50;

  double change = money - price;

  print('Your change is ${change.toStringAsFixed(2)} USD');
}

check here

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

Comments

1

I would use the intl package from google

var formatter = new NumberFormat.currency(locale: "en_US", symbol: "$");
print('Your change is {formatter.format(change)} USD');

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.