Results 1 to 1 of 1

Thread: How does QLocale::toString(double i, char f, int prec) round?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How does QLocale::toString(double i, char f, int prec) round?

    Hi,

    we store certain values with 4 decimal places in our database. In one method I need to convert the sum of two of these values to a QString with 2 decimal places using QLocale::toString. In another method I need to convert the same sum to an integer (= sum*100 with last two digits same as 2 decimal places in the QString).

    Code used in the first method:
    QLocale::system().toString(value1 + value2, 'f', 2);

    Second method:
    qRound((value1 + value2) * 100.00);

    In most cases this works, but every once in a while there is a difference of 1 between the last digit in the integer and 2nd decimal place in the QString. So I guess QLocale::toString doesn't round exactly like qRound. I have taken a look at the source code, but couldn't find out more.
    Any suggestions?


    Edit:
    Here are some samples where that problem occurs:

    -
    toString(37.485, 'f', 2) = 37,48
    qRound(3748.5) = 3749

    -
    toString(169.575, 'f', 2) = 169,57
    qRound(16957.5) = 16958

    So does QLocale::toString generally just cut off the unnecessary decimal places without rounding?
    Last edited by ChiliPalmer; 10th August 2010 at 11:46.

Similar Threads

  1. round double
    By wirasto in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2009, 08:32
  2. QHostAddress::toString
    By spraff in forum Qt Programming
    Replies: 0
    Last Post: 22nd November 2008, 16:53
  3. Replies: 4
    Last Post: 20th December 2007, 11:51
  4. QLocale/Resource
    By coderbob in forum Newbie
    Replies: 4
    Last Post: 21st November 2007, 20:51
  5. QLocale confusion :(
    By gri in forum Qt Programming
    Replies: 6
    Last Post: 15th June 2007, 13:09

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
  •  
Qt is a trademark of The Qt Company.