Suppose a
= 3.91900007534
.
In C language, I want to store the value of the variable a
in another variable, r
, up to two decimal places, such that r
= 3.92
.
Note that I don't want to print the value up to two decimal places, I just want to store the value as I need the exact value of r
for the next operation.
Asked
Active
Viewed 2,989 times
1

Gabriel Staples
- 36,492
- 15
- 194
- 265

Imtiaz
- 25
- 2
- 7
-
2C does not have a native decimal type. – stark Feb 25 '21 at 16:18
-
Related: fixed point math in C (my answer): https://stackoverflow.com/a/53936802/4561887 – Gabriel Staples Feb 25 '21 at 16:23
-
3I haven't checked but it's highly unlikely that you would be able to store *exactly* `3.92` on any binary computer. See e.g. [Why are floating point numbers inaccurate?](https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate). – Some programmer dude Feb 25 '21 at 16:26
-
If you need the _exact_ value you will probably need to store it scaled up by a factor of 100 and rounded to the nearest integer. – Ian Abbott Feb 25 '21 at 19:18
2 Answers
1
Simply, just multiply by 100, truncate the number with floor
and divide by 100:
double convert( double in ){
return floor(in*100)/100.0;
}
More generic approach:
#include <math.h>
double convert( double in, int decimals ){
double aux = pow(10,decimals);
return floor(in*aux)/aux;
}
As @Some programmer dude said, the above code truncates. If you wisth to round, just replace floor()
by round()
:
double convert( double in ){
return round(in*100)/100.0;
}
Here the code running: https://onlinegdb.com/By-a1Urf_

Daniel Argüelles
- 2,229
- 1
- 33
- 56
1
The quickest way I can think of doing it is using the following method, ie multiplying by 100, rounding and then dividing again by 100:
int main()
{
float number = 1.2345672;
// Setting precision to 2 digits
number = round(number*100)/100;
printf("%g", number);
return 0;
}
Of course, if you wanted 3 decimal points, it would be 1000, rathern than 100.
So we can make a very good general function, like this:
double precisionSetter(double num, double precision)
{
return round(pow(10,precision)*num)/pow(10,precision);
}
So you can easily choose how many decimal places you want it to:
double a = 1.234567
a = precisionSetter(a,2)
The above will round the float. If you are interested in truncating said float, use the floor()
function rather than round()
one.

BiOS
- 2,282
- 3
- 10
- 24
-
-
That's a good point! I have edited my answer to take account of that. Thanks! – BiOS Feb 25 '21 at 16:35
-
1This would truncate rather than round. To round, add 0.5 to the multiplied result prior to flooring. – Gabriel Staples Feb 25 '21 at 16:36
-
1
-
1Thanks for your contribution @Gabriel. The adding 0.5 trick is also a good one! – BiOS Feb 25 '21 at 16:42
-
1@BiOS, the same trick works during integer division. For a positive integer `numerator/denominator`, just doing `numerator/denominator` truncates, but doing `(numerator + denominator/2)/denominator` rounds during division, because `denominator/2` is the fractional integer equivalent to the floating point `0.5`. For numerators and denominators that can be negative, it gets more complicated, but here is some code I wrote with macros showing how to do that too: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/c/rounding_integer_division/rounding_integer_division.cpp. – Gabriel Staples Feb 25 '21 at 16:54
-
I have tried both of the mentioned methods, but final stored value becomes 3.920000 not 3.92. How to remove those trailing zeros? – Imtiaz Feb 25 '21 at 16:55
-
1Mukkaddas, as the default floating point precision in C is 6 digits after the decimal point, you are left with formatting here I am afraid. You could print your number with the `"%g"` formatter, which will do this automatically for you. I have edited my answer to let you test it. Differently, you could rever to a classical approach: `"%.2f"` – BiOS Feb 25 '21 at 17:08
-
@Gabriel Staples, that's great! Thanks for sharing that, those are spot on examples! – BiOS Feb 25 '21 at 17:11
-
@Mukaddas Imtiaz Himel, you can't. If this is a homework problem, that's probably the entire point of this exercise: to learn that base 2 numbers cannot perfectly represent all simple base 10 numbers. This is called numerical error, or computational precision error, or something like that, and you can't get rid of it. You can only mitigate it by using larger and larger and larger types. `long double` has it less than `double` which has it less than `float`, since `float` might be 32 bits, `double` 64 bits, and `long double` 128 bits, depending on your computer architecture. – Gabriel Staples Feb 25 '21 at 17:13
-
Let me clarify my last comment: I'm assuming that at the end of all of those zeros there are non-zero numbers as well, at the point numerical error comes in. Also, @Mukaddas, keep in mind that the number 1 is actually 1.000000000000000000... with infinite repeating zeros. You could even put infinite preceding zeros and say it is ...000000000000000000001.000000000000000000... with infinite preceding and trailing zeros. All numbers are like this. You don't have to write all the infinite preceding and trailing zeros. – Gabriel Staples Feb 25 '21 at 17:18