bevo009
bevo009

Reputation: 523

Limiting decimal places in print output, in Dart

Sorry, noob question I'm searching for how to limit the amount of decimal points printed. I found this:

print(price.toStringAsFixed(2));    // which worked great in another script

but how do I limit to 1 decimal point in the script below? I'm getting 14 decimal places :)

String inTemp = stdin.readLineSync();
int temp = int.parse(inTemp);

switch (selection) {
  case 'A':
    print('$temp degrees Celcius is ${temp * 1.8 + 32} degrees Fahrenheit');

*I'm sure it's something obvious but I can't figure it out.

Cheers for any help.

** Edit: So I can do it by adding new variables like below, but I'd still like to know how/if it can be done in the original print statement.

String inTemp = stdin.readLineSync();
  int temp = int.parse(inTemp);
  num temp1 = (temp * 1.8 + 32);
  num temp2 = ((temp - 32) / 1.8);

  switch (selection) {
    case 'A':
      print('$temp degrees Celcius is ${temp1.toStringAsFixed(1)} degrees Fahrenheit'); 

Upvotes: 2

Views: 2205

Answers (1)

lrn
lrn

Reputation: 71828

You do the same thing to the number, just inside the ${...}:

case 'A':
  print('$temp degrees Celcius is ${(temp * 1.8 + 32).toStringAsFixed(1)}'
        ' degrees Fahrenheit');

Upvotes: 2

Related Questions