0

First of all, I want to say sorry because I think the doubt is so trivial... but I'm new programming in C++.

I have the following code:

int a = 234;
int b = 16;
float c = b/a;

I want to print the result from float c with 2 decimals (the result should be 0.06) but I don't get the expected result.

Can anyone can help me? I tried using CvRound() and setPrecision() but nothing works like I expect or, in my case, I don't know how to do them working.

James Taylor
  • 6,158
  • 8
  • 48
  • 74
  • Change a to float or cast it during operation `float c = b/(float)a;` – Kegluneq Oct 05 '15 at 11:11
  • 3
    The key problem is that the result of `b/a` is an `int`. You'll need to convert at least one to a `float` or `double` before you divide. Once you do that your next step is to consider that floating points can't represent all results exactly. – Dietmar Kühl Oct 05 '15 at 11:12

4 Answers4

3

The problem is actually blindingly simple. And has NOTHING whatsoever do do with settings such as precision.

a and b are of type int, so b/a is also computed to be of type int. Which involves rounding toward zero. For your values, the result will be zero. That value is then converted to be float. An int with value zero, when converted to float, gives a result of 0.0. No matter what precision you use to output that, it will still output a zero value.

To change that behaviour convert one of the values to float BEFORE doing the division.

 float c = b/(float)a;

or

 float c = (float)b/a;

The compiler, when it sees a float and and an int both participating in a division, converts the int to float first, then does a division of floats.

Peter
  • 35,646
  • 4
  • 32
  • 74
0
int a = 234;
int b = 16;
float c = b/(float)a;
float rounded_down = floorf(c * 100) / 100;   /* floor value upto two decimal places */
float nearest = roundf(c * 100) / 100;  /* round value upto two decimal places */
float rounded_up = ceilf(c * 100) / 100;      /* ceiling value upto two decimal places */
amit dayama
  • 3,246
  • 2
  • 17
  • 28
0

try this:

#include <iostream>
#include <iomanip>

using namespace std;

int main(){
   int a = 234;
   int b = 16;
   float c = float(b)/a;
   cout<<fixed<<setprecision(2)<<c;
   return 0;
}

Previously when c = b/a since a and b are integers so by integer division we were getting answer as 0 as per your program.

But if we typecast one of the variable(a or b) to float we will obtain decimal answer.

Number of digits after decimal may or may not be 2. To ensure this we can use fixed and setprecision. fixed will ensure that number of digits after decimal will be fixed. setprecision will set how many digits to be there after decimal.

cryptomanic
  • 5,986
  • 3
  • 18
  • 30
  • Could you please [edit] in an explanation of why this code answers the question? Code-only answers are [discouraged](http://meta.stackexchange.com/q/148272), because they don't teach the solution. – NathanOliver Oct 05 '15 at 15:02
0

If you just want to print the result, you can use a printf() formatting string to round:

printf("c = %.2f\n", number, pointer);

Otherwise, if you need c to calculate another value, you shouldn't round the original value, only the one to print.

James Taylor
  • 6,158
  • 8
  • 48
  • 74