18

I have a float number for example 12.12123 Is there a function which would display only number with 2 digits after decimal point 12.12 ?

Here is the code:

y1 = ( c1 - (a1 * x)) / b1;
 y2 = ( c2 - a2 * x) / b2;

if (y1 == y2)
  cout << "The same";

so if the y1 = 1.001 and the y2 = 1.002 they do not appear as the same.

I tried to add. cout.setf(ios::fixed, ios::floatfield); cout.precision(2);

but it does not seem to help.

Gerret
  • 2,948
  • 4
  • 18
  • 28
user474401
  • 181
  • 1
  • 1
  • 4

7 Answers7

40
/* The C way */
#include <stdio.h>
...
float f = 12.12123f;
printf("%.2f",f);

// The C++ way
#include <iostream>
...
float f = 12.12123f;
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout.precision(2);
std::cout << f;

// The alternative C++ way
#include <iostream>
#include <iomanip>
...
float f = 12.12123f;
std::cout << std::fixed << std::setprecision(2) << f;

In C, the 0 padding is added automatically to the right if there are not enough digits to print. In the C++ examples, instead, this is disabled; to enable this behavior, you should enable the fixed mode on the stream with std::fixed (or enabling the relevant stream flags with std::ios_base::setf()).

Edit: I remembered wrong; if fixed is not set, the precision setting says to the stream the total number of digits to display, including also the ones before the decimal point. So, in this case I think that the only way is to use the fixed mode (examples fixed), which will yield the same behavior of printf.


Links:

Dennis
  • 56,821
  • 26
  • 143
  • 139
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
9

You're looking for printf("%.2f", 12.12123); or:

#include <iostream>
#include <iomanip>

using namespace std;
cout << fixed << setprecision(2) << 12.12123;

EDIT: Question changed, so does the answer.

You never want to use direct equality with floating point, you always compare within epsilon tolerance. Your epsilon is just quite large.

Replace if (y1 == y2) with if (abs(y1 - y2) < 0.01).

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • What it does it output the result in console, but how can I change the value of the variable y1 ? – user474401 Oct 13 '10 at 11:58
  • y1 = ( c1 - (a1 * x)) / b1; cout << fixed << setprecision(2) << y1; – user474401 Oct 13 '10 at 11:59
  • 1
    You can do `y1 = floor(y1 * 100 + 0.5) / 100;` but it would be meaningless: [floating-point precision](http://en.wikipedia.org/wiki/Floating_point) cannot represent all decimal numbers, in fact your example `12.12` is approximated as `12.119999999999999` on my platform. – Frédéric Hamidi Oct 13 '10 at 12:05
3
double num = 1.4567;
printf("%.2f",num);

This would print 1.46

patentfox
  • 1,436
  • 2
  • 13
  • 28
2
cout.precision(2);
cout <<f;
dutt
  • 7,909
  • 11
  • 52
  • 85
2
#include <iostream>
#include <iomanip>

int main ()
{
   double d = 1.242354345; 
   using namespace std;
   cout << setiosflags(ios::fixed | ios::showpoint)
        << setprecision(2)
        << d; 
}
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
2

You are probably asking wrong question. Try following:

double diff = fabs(y1-y2);
if(diff < 0.005)
    cout << "Almost same";
patentfox
  • 1,436
  • 2
  • 13
  • 28
1
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
float al = 42.645; //sample
cout << al;
Ruel
  • 15,438
  • 7
  • 38
  • 49