0

I'm trying to calculate the number 4.2 + 0.2 - 0.1 However Despite having the value of 4.3 the output was 4.300000000000001.

This is the animal.dart class

class Animal {
  String name = '';
  int age;
  double weight = 0;

  Animal(this.name, this.age, this.weight);

  void eat() {
    print('$name is eating.');
    weight = weight + 0.2;
  }

  void sleep() {
    print('$name is sleeping.');
  }

  void poop() {
    print('$name is pooping.');
    weight = weight - 0.1;
  }
}

and this is the main() function

import "dart:io";
import "lorem.dart";


void main(){
var pet = Animal('Flowie', 2, 4.2);

  pet.eat();
  pet.poop();
  print(pet.weight);
}

and the output is the following:

Flowie is eating.
Flowie is pooping.
4.300000000000001

what's the correct way to output the weight to 4.3 instead of 4.300000000000001 ?

0 Answers0