简体   繁体   中英

correct input value with toFloat() in Qt

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM