thnkwthprtls
thnkwthprtls

Reputation: 3487

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!

Upvotes: 23

Views: 77404

Answers (1)

ratchet freak
ratchet freak

Reputation: 48176

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);

Upvotes: 46

Related Questions