Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Rounding a number when the last digit is 5
Forum Updated to NodeBB v4.3 + New Features

Rounding a number when the last digit is 5

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 238 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by
    #1

    Rounding the following

    QString value= "0.3145675"; 
    qDebug()<<"Value after rounding ="<<QString::number(value.toDouble(),'f',m_decimals);  //Output - 0.314567
    

    For such cases , the answer should be 0.314568 as per the rule Keep it EVEN

    How can it be achieved?

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    kshegunovK 1 Reply Last reply
    0
    • Swati777999S Swati777999

      Rounding the following

      QString value= "0.3145675"; 
      qDebug()<<"Value after rounding ="<<QString::number(value.toDouble(),'f',m_decimals);  //Output - 0.314567
      

      For such cases , the answer should be 0.314568 as per the rule Keep it EVEN

      How can it be achieved?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      The behavior is correct:
      https://www.binaryconvert.com/result_double.html?decimal=048046051049052053054055053

      0.3145675 isn't exactly representable.

      @Swati777999 said in Rounding a number when the last digit is 5:

      How can it be achieved?

      By understanding how floating point numbers work and how they are represented.
      https://en.wikipedia.org/wiki/Double-precision_floating-point_format

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      5

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved