# Thread: How to not lose precision when dividing and assigning to integer?

1. ## How to not lose precision when dividing and assigning to integer?

Hi all,

I want to find a way of being able not to lose precision when I am doing a division and then putting the result to an integer. How can I do that?

Regards

2. I think you are wanting to store digits after the decimal place, like 4/3 = 1.3333..., right?

In that case, you'll have to store the result as a floating point number - integers can't represent that kind of data. Just introduce a float or double and use casts, like this:

Code:
```int a = 4, b = 3;
double ans;

ans = (double) a / (double) b; //ans = 1.3333...```

3. You can use round(), but be careful with the casting -- you obviously need to cast the return value to an int, but you also need to cast the submitted equation to a double:
Code:
```#include <math.h>
int a = 5, b = 3, n = (int)round((double)a/b);```
This will yield n = 2 (rounded up from 1.66..) instead of the usual 1.

As bernt points out, integers in C do not have any decimal places, but then, that is also the definition of integer in "real life".

4. If you are only representing ratios of integers, then you can just store the numerator and denominator in two variables, don't actually divide anywhere, and use the rules of fractional arithmetic.