Results 1 to 3 of 3

Thread: Qt and decimal number

  1. #1
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    1
    Qt products
    Qt5

    Default Qt and decimal number

    Hello. I have this fraction:

    Qt Code:
    1. 225/222
    To copy to clipboard, switch view to plain text mode 

    the result is this:

    Qt Code:
    1. 1,0135135135135135135135135135135
    To copy to clipboard, switch view to plain text mode 

    From this decimal number, it's interesting to me to extract this part

    Qt Code:
    1. 1,013
    To copy to clipboard, switch view to plain text mode 

    Then be able to process at a later time. You can do that? If so, how?

    Thank you in advance

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qt and decimal number

    Qt Code:
    1. #include <cmath>
    2. double result = 225.0/222.0;
    3. double truncatedResult = std::floor(result * 1000) / 1000.0;
    To copy to clipboard, switch view to plain text mode 
    Be aware that both results are approximate and continuing to calculate with the truncated result may accumulate errors.
    If the result can be negative check how this logic works in your application.

  3. The following user says thank you to ChrisW67 for this useful post:

    Imhotep (19th September 2016)

  4. #3
    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: Qt and decimal number

    From this decimal number, it's interesting to me to extract this part
    Are you sure you aren't confusing how you display the number in your GUI with how you are using the number in your calculations? It's fine to take an irrational number and truncate it for display, but as ChrisW67 says, it probably is not OK to truncate it and then use it for calculations. Every time you use the truncated version, the error in your calculation increases.

    Qt Code:
    1. 225 / 222 = 1.0135135135...
    2. (225 / 222) ^2 = 1.02720964...
    3. (225 / 222) ^3 = 1.0409085...
    4.  
    5. 1.013 ^2 = 1.026
    6. 1.013 ^3 = 1.039
    To copy to clipboard, switch view to plain text mode 

    Of course, back in 1970, the scientists and engineers put men on the moon using their slide rules with 3-decimal place accuracy...
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 6
    Last Post: 30th September 2015, 21:20
  2. Decimal to Binary conversion
    By anh5kor in forum Newbie
    Replies: 1
    Last Post: 5th February 2015, 13:16
  3. Replies: 1
    Last Post: 5th March 2012, 07:34
  4. modifications of decimal separator
    By 21did21 in forum Qwt
    Replies: 2
    Last Post: 2nd August 2011, 22:41
  5. Formatting for two decimal places
    By TomJoad in forum Newbie
    Replies: 1
    Last Post: 4th April 2011, 17:56

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.