tags:

views:

164

answers:

6

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;
}  
+13  A: 

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
Mala
Right, in effect this removes the third digit after decimal point.
ShaChris23
+2  A: 

Simple: 0.999 rounded to two decimal places is 1.00.

Jonathan
Is there something else provided by STL that will just retain 0.99 without rounding up or down?
ShaChris23
A: 

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.

kkress
Why split the integral and fractional parts? Why not just multiply-truncate-divide the entire thing at once?
Martinho Fernandes
Multiplying by 100 might cause an overflow if the float is large. (Yeah... not a terribly compelling argument I know)
kkress
If the number is large enough to overflow when multiplied by 100, getting the fractional part is meaningless, as you don't have that much precision.
Martinho Fernandes
A: 

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.

ShaChris23
Because I dont like this solution, so I'm asking people for advice.
ShaChris23
Does QString::number round? If so, what if value is 0.9999? If not, why not use 2 as the precision?
Martinho Fernandes
Great question, Martinho. QString does round if you dont specify enough precision. For example, if I did `QString::number(number, 'f', 2), and `number` is 0.9999, it would round to 1.0.
ShaChris23
Do you mean if the `value` variable was 0.9999 or if the `number` variable was 0.9999? I will just assume you meant `number`. If `number` was .9999, after `chop(1)`, the string would be `9.99` which is what I want (2 digits after decimal).
ShaChris23
I used 3 as the precision to prevent it from rounding up when `value` is 999.9f, and then the `chop(1)` chops the 3rd digit so that I would only have 2 digits after the decimal.
ShaChris23
cout << setprecision(2) << fixed << (numberToDisplay-0.005) << endl;
Russell Borogove
+4  A: 

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;
Michael Anderson
A: 

Just another one to throw out there:

#include <cmath>

std::cout << numberToDisplay - std::fmod(numberToDisplay, 0.01f) << std::endl;
dreamlax
You should keep the `setprecision(2)` part. Say, you're trying to display `0.019` as `0.01`. It happens that `0.01` is not representable by IEEE754 floating point values, so the result of your subtraction will never be `0.01`. It is likely you will see some gibberish like 0.009999999776482582092285156250.
Martinho Fernandes
@Martinho: Your example works for me without the `setprecision(2)`, but +1 for the advice.
dreamlax
@Martinho: cout has some default precision, so you shouldn't see the gibberish part by default.
visitor