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!
Here's for an int: int i = 42; QString s = QLocale(). toString(i);
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With