Can someone explain me how to choose the precision of a float with a C function?
Examples:
theFatFunction(0.666666666, 3)
returns 0.667
theFatFunction(0.111111111, 3)
returns 0.111
You can't set precision of a float or double variable. Only for the input/output of such a variable. But you shouldn't be using float or double for monetary values to begin with, due to the fact that floating-point values/math is inprecise.
A float has 23 bits of mantissa, and 2^23 is 8,388,608. 23 bits let you store all 6 digit numbers or lower, and most of the 7 digit numbers. This means that floating point numbers have between 6 and 7 digits of precision, regardless of exponent.
You can't do that, since precision is determined by the data type (i.e. float
or double
or long double
). If you want to round it for printing purposes, you can use the proper format specifiers in printf()
, i.e. printf("%0.3f\n", 0.666666666)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With