0

I have a problem with calculate with Double in Flutter.

This is my code:

double depth = 0.5;

                        RoundIconButton(
                          icon: FontAwesomeIcons.plus,
                          onPressed: () {
                            setState(() {
                              depth = depth + 0.1;
                            });
                          },
                        ),

The Outcome comes in a textfield

                    Text(
                      depth.toString(),
                      style: kBigTextstyle,
                    ),

When I push the plus button, it shows:

0.6

then 0.7

en then 079999999999999

But is has to be 0.8

                              depth = depth + 0.10000;

double depth = 0.5;

                        RoundIconButton(
                          icon: FontAwesomeIcons.plus,
                          onPressed: () {
                            setState(() {
                              depth = depth + 0.1;
                            });
                          },
                        ),

The Outcome comes in a textfield

                    Text(
                      depth.toString(),
                      style: kBigTextstyle,
                    ),

I expect the output of 0.7 + 0.1 to be 0.8 but the actual output is 0.7999999

Margriet
  • 197
  • 2
  • 10
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Richard Heap Jun 16 '19 at 12:24

1 Answers1

1

Instead of writing

depth.toString()

please write

depth.toStringAsFixed(1)

onosecond
  • 548
  • 6
  • 10