Maxbester
Maxbester

Reputation: 2533

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...

Upvotes: 4

Views: 3543

Answers (1)

cloose
cloose

Reputation: 956

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

Upvotes: 9

Related Questions