简体   繁体   中英

Format of float to QString conversion

I would like to convert a float to a QString but replacing the . by a , .

For example, I want the float 12.95 to be converted to a QString that looks like 12,95 .

I guess I can do it with something like:

QString().sprintf("%something", myFloat);

But how should I write instead of %something ?

Maybe I can do it like this: QString::number(myFloat, 'f').replace(".", ",") but it is not very pretty...

Did you try QLocale::toString() already?

The following code should return the float with comma as decimal separator:

QLocale german(QLocale::German, QLocale::Germany);
QString s1 = german.toString(12.95, 'f');

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