sivabudh
sivabudh

Reputation: 32635

Display 0.999 as 0.99

How can I display a float number = 0.999 as 0.99?

The code below keeps printing out 1.00 ? I thought using setprecision(2) specifies the number of digits after the decimal point?

#include <iostream>
#include <iomanip>

using namespace std;


int main(int argc, char** argv) 
{     
  const float numberToDisplay = 0.999;
  cout << setprecision(2) << fixed << numberToDisplay << endl;

  return 0;
}  

Upvotes: 3

Views: 2205

Answers (6)

dreamlax
dreamlax

Reputation: 95335

Just another one to throw out there:

#include <cmath>

std::cout << numberToDisplay - std::fmod(numberToDisplay, 0.01f) << std::endl;

Upvotes: 0

Michael Anderson
Michael Anderson

Reputation: 73480

I'd use floorf, as I feel it expresses your intent better than some of the other solutions.

cout << setprecision(2) << fixed << floorf(numberToDisplay*100)/100 << endl;

Upvotes: 4

kkress
kkress

Reputation: 807

One way to do this is to split the original value into its integer and decimal parts, multiply the decimal part by 100 (since you want only 2 digits) and then split that again to get only the 'integer' portion of that number. Its not terribly elegant, but it does work:

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

int main(int argc, char** argv) 
{
   const double numberToDisplay = 0.999;

   double origInteger;
   double origDecimal;

   modf(numberToDisplay, &origInteger);

   double decimal = numberToDisplay - origInteger;

   //prints .999 even if the number is 12.999
   cout << decimal << endl;

   //results in 99 in origDecimal
   modf(decimal * 100, &origDecimal);
   //integer + .99
   double final = origInteger + (origDecimal / 100);

   cout << final << endl;

   return 0;
}

Edit: casting to (int) is far simpler as described in another answer.

Upvotes: 0

sivabudh
sivabudh

Reputation: 32635

Okay, just wanted to share a solution that I came up with:

Here's how I solved the problem:

float const number = value / 1000.0f;
QString string     = QString::number(number, 'f', 3);
string.chop(1);

Basically, the algorithm is:

  • Convert the floating point number to be a string retaining 3 digits after decimal points
  • Chop the last character from the string

The flaw with this approach is the chopping and having to specify 3.

I use the same logic for one million and one giga (10^9) as well, and I have to change precision value to be 6 and 9, and chop value to be 4 and 7 respectively.

Upvotes: 0

Mala
Mala

Reputation: 14803

setprecision(2) will round to the nearest two-digit floating point number, in this case 1.0. If you wanted to truncate (i.e. get 0.99) you could always multiply the number by 100 (i.e. 10^[num-digits]), cast to an int, and then divide it back into a float. A little messy but it gets the job done.

const float numberToDisplay = 0.999;
const float numberTruncated = (int)(numberToDisplay * 100) / 100.0;
// float numberTruncated is 0.99

Upvotes: 13

Jonathan
Jonathan

Reputation: 1507

Simple: 0.999 rounded to two decimal places is 1.00.

Upvotes: 2

Related Questions