Results 1 to 10 of 10

Thread: How can i convert qstring to money format ?

  1. #1
    Join Date
    Apr 2009
    Location
    İstanbul, Türkiye
    Posts
    26
    Thanks
    1

    Default How can i convert qstring to money format ?

    How can i convert qstring to money format ?

    Ex: 1234567.89 = 1,234,567.89
    Last edited by newermind; 18th May 2009 at 12:04.

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can i convert qstring to money format ?

    float QString::toFloat ( bool * ok = 0 ) const

  3. #3
    Join Date
    Apr 2009
    Location
    İstanbul, Türkiye
    Posts
    26
    Thanks
    1

    Default Re: How can i convert qstring to money format ?

    i try to seperate with comma`s.

    toFloat function is not worked.

    Example

    QString money = 1234.00

    i try to
    money = 1,234.00

    How can i do it with using toFloat function?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How can i convert qstring to money format ?

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

  5. The following user says thank you to Lykurg for this useful post:

    newermind (18th May 2009)

  6. #5
    Join Date
    Oct 2006
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can i convert qstring to money format ?

    Try this:

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

  7. #6
    Join Date
    Apr 2009
    Location
    İstanbul, Türkiye
    Posts
    26
    Thanks
    1

    Default Re: How can i convert qstring to money format ?

    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.

  8. #7
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How can i convert qstring to money format ?

    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()).
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  9. #8
    Join Date
    May 2009
    Posts
    11
    Thanks
    3

    Default Re: How can i convert qstring to money format ?

    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.

    http://doc.trolltech.com/4.3/qlineed...inputMask-prop

  10. #9
    Join Date
    Apr 2016
    Posts
    1
    Platforms
    Unix/X11

    Default Re: How can i convert qstring to money format ?

    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.

  11. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How can i convert qstring to money format ?

    Qt Code:
    1. QString myString;
    2. myString=ui->total_lineEdit->text ();
    3. myString.toDouble ();
    4. ui->total_lineEdit->setText (myString);
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. void MyWidget::onEditingFinished()
    2. {
    3. QString myString = ui->lineEdit->text();
    4. bool didItWork;
    5. double dValue = myString.toDouble( &didItWork );
    6. if ( didItWork ) // didItWork would be false if the string couldn't be converted. What if the user didn't enter a number?
    7. {
    8. QString myNewString = QString( "%1" ).arg( dValue, 7, 'f', 2 );
    9. ui->lineEdit->setText( myNewString );
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

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

Similar Threads

  1. convert from QString to char[sizeof(...)]
    By adamatic in forum Qt Programming
    Replies: 4
    Last Post: 3rd September 2011, 09:05
  2. convert QString to QByteArray
    By morgana in forum Qt Programming
    Replies: 5
    Last Post: 2nd March 2011, 13:33
  3. cannot convert all images to PNG format ...
    By sh123 in forum Qt Programming
    Replies: 2
    Last Post: 21st March 2009, 13:04
  4. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48
  5. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.