1

I want to change text into float number for calculation in Qt

float test = ui.tableWidget->item(0, 1)->text().replace(",", ".", Qt::CaseSensitive).toFloat();

when I type 2.86, actually I see test = 2.85999990 in debug. How can I receive correctly what i typed ?

I can use this for showing

qDebug() << QString::number( test, 'f', 2 );

But I want a value for calculation, not for showing.

songvan
  • 369
  • 5
  • 21
  • 3
    Unfortunately floating point numbers won't give you much more precise results. This is the way they are - you usually get the closest number that can be expressed in *some size* floating point number. I'd recommend reading some wikipedia on the reasons why (take a look at the equations there). Note: `double` could give you slightly better results. – hauron Jul 25 '16 at 13:53
  • @hauron: thank you. Yes, I have checked with `double`, but the result is just slightly better as you said. Do we have any way to receive exactly what we want ? – songvan Jul 25 '16 at 14:23

1 Answers1

1

As @hauron already said this is format specific but normal behaviour. You might want to take a read on this.

Additionally you might also take a look at another answer here. Using some additional library may work for you, if applicable.

Community
  • 1
  • 1
maxik
  • 1,053
  • 13
  • 34