Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - Converting float to QString

This seems like it should be very simple, but I'm going through the docs and not seeing anything. I just need to convert a number that is represented as a float object into a QString object. I know there is the function QString::number() that can be used for other types like int and double, like so: int a = 1; QString b = QString::number(a);

...however this doesn't seem to work for float. Perhaps there is some way where it is converted first from float to another type, and then from that type to QString? If anyone has any ideas I'd appreciate it. Thanks!

like image 232
thnkwthprtls Avatar asked Mar 13 '14 15:03

thnkwthprtls


People also ask

How do you convert int to QString?

Here's for an int: int i = 42; QString s = QLocale(). toString(i);

How do you convert QString to hex in Qt?

Try QString::toInt, QString::toUInt, QString::toLong etc., for example: The second argument is the base, 16 in this case for hexadecimal. This solution will work if your string fits into a unsigned long long or shorter - it will not work if you want to convert arbitrarily long strings that way.


1 Answers

float will auto-promote to double when needed

float pi = 3.14; 
QString b = QString::number(pi);

should work

otherwise you can use setNum:

float pi = 3.14; 
QString b;
b.setNum(pi);
like image 166
ratchet freak Avatar answered Sep 22 '22 19:09

ratchet freak