Results 1 to 3 of 3

Thread: Remove traling zeros from QString scientific notation num value

  1. #1
    Join Date
    Apr 2020
    Posts
    1

    Default Remove traling zeros from QString scientific notation num value

    I have the next code, this code print a value in scientific notation, but i want to remove the trailing zeros

    valor = 541; QString cStyleResult = QString::number(valor.toDouble(), 'e');

    qDebug() << cStyleResult ;

    //////////////////////////////////////////////
    output: 5.410000e2

    I want to remove the trailing zeros(right decimal zeros), example I want to save only the value of 5.41

    some ideas????

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    516
    Thanks
    12
    Thanked 77 Times in 75 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Remove traling zeros from QString scientific notation num value

    Hi, you could
    - specifiy a precision as the third parameter for QString::number
    - you could use QString::arg() and play with fieldwidth and precision
    - you could take your string, find the e or E of the scientific notation, and remove all zeroes in front of it (probably the ugliest method...)
    - probably many more...

    Ginsengelf

  3. #3
    Join Date
    Jan 2025
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Remove traling zeros from QString scientific notation num value

    QString cStyleResult = QString::number(valor, 'e', 6); // Set precision to 6.
    cStyleResult = cStyleResult.remove(QRegExp("0+$")); // Remove trailing zeros.
    cStyleResult = cStyleResult.remove(QRegExp("\\.$")); // Remove trailing dot if any.
    This should give you the desired output without trailing zeros.

    Moderator's note: Spam link removed. No more warnings, you will be banned.
    Last edited by d_stranz; 7th January 2025 at 17:56. Reason: removed URL

Similar Threads

  1. Replies: 1
    Last Post: 9th March 2019, 10:15
  2. Remove \n from QString
    By arturs in forum Newbie
    Replies: 4
    Last Post: 20th April 2015, 09:41
  3. Replies: 3
    Last Post: 17th April 2010, 22:35
  4. qdoublespinbox with scientific notation
    By pospiech in forum Qt Programming
    Replies: 13
    Last Post: 3rd January 2009, 15:50
  5. QString:: Padding Zeros?
    By Harvey West in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2007, 19:25

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.