luhluh
luhluh

Reputation: 187

How to display two decimal values in Dart/Flutter?

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');
}

Upvotes: 2

Views: 3103

Answers (2)

dave
dave

Reputation: 64657

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');

Upvotes: 1

Sandeep Sharma
Sandeep Sharma

Reputation: 705

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

Upvotes: 4

Related Questions