View Full Version : How can i convert qstring to money format ?

18th May 2009, 12:46
How can i convert qstring to money format ?

Ex: 1234567.89 = 1,234,567.89

18th May 2009, 12:57
float QString::toFloat ( bool * ok = 0 ) const

18th May 2009, 13:14
i try to seperate with comma`s.

toFloat function is not worked.


QString money = 1234.00

i try to
money = 1,234.00

How can i do it with using toFloat function?

18th May 2009, 13:32
Don't know if QLocale offers a conversion to a money format, but you could simply write your own little function which does the formatting. Just use loops and the function QString provides. (Or you use a QRegExp...)

19th May 2009, 09:37
Try this:

double dValue = 123467.99;
QString sValue = QString("%L1").arg(dValue ,12,'f',0,' ');

19th May 2009, 09:50
Actual question that i wanna ask is that, I have a QLineEdit, and when user enter some number into it, i want to replace that string with its currency represantation.

I tried to change the input on textChanged(const QString&) signal on that QlineEdit object, but i had involved lots of problems.

IMHO, There must be easy way to tell QLineEdit, use that locale to change numbers into currency.

19th May 2009, 13:15
I would try to make my own validator (derived from QValidator) that will check if with some regexp if line edit contains appriopriate value and fix it/change it to the currency format (QValidator::fixup()).

19th May 2009, 13:45
if I'm looking on a lineedit in the qt creator one of it's properties is the inputmask,
I think that it's what you're looking for.


17th April 2016, 20:24
Wow old thread but I have some solutions.
So the problem is this you are going to have to change the text from your lineEdit to a QString object to use a similar myVariable.toDouble function.

Lets say I have a lineEdit called total_lineEdit. I would try this.

QString myString;
myString=ui->total_lineEdit->text ();
myString.toDouble ();
ui->total_lineEdit->setText (myString);

The problem I have is I need to setPrecision but looking for a QT version of this. I'm just learning C++ and QT at the same time and it does get confusing.

17th April 2016, 23:19
QString myString;
myString=ui->total_lineEdit->text ();
myString.toDouble ();
ui->total_lineEdit->setText (myString);

How is this a "solution"? Do you understand that your code does absolutely nothing to the string or the line edit contents? Line 1 declares a temporary QString instance. Line 2 retrieves the text into that string. Line 3 converts the string contents to a double, then throws the double away. Line 4 puts the unmodified string back into the line edit. Obviously, you are confused.

So what I am assuming you want to do is something like this: The user enters a string "1234.5" and you want it displayed with two digits of precision after the user is done: "1234.50". Then in your slot that handles the QLineEdit::editingFinished() signal, you could do something like this:

void MyWidget::onEditingFinished()
QString myString = ui->lineEdit->text();
bool didItWork;
double dValue = myString.toDouble( &didItWork );
if ( didItWork ) // didItWork would be false if the string couldn't be converted. What if the user didn't enter a number?
QString myNewString = QString( "%1" ).arg( dValue, 7, 'f', 2 );
ui->lineEdit->setText( myNewString );

Look at the documentation for the QString::arg() version that takes a double as the first argument for an explanation of the formatting arguments.