1

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');
}
luhluh
  • 167
  • 1
  • 11
  • 1
    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](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). – jamesdlin Apr 25 '20 at 02:04

2 Answers2

3

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

Sandeep Sharma
  • 639
  • 2
  • 9
  • 34
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');
dave
  • 62,300
  • 5
  • 72
  • 93