How to set precision in float values in C++ in division operation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to set precision in float values in C++ in division operation

Suppose I have 2 numbers, A and B, both float type. I have to divide them up and store the result in another float variable C. So, C=A/B, but I want C to contain a decimal number with upto 10 decimal places, instead of the default 4 to 5 decimal places. And how to do that??? I know there's a setprecision function, but have seen it only been used it in cout and not for storing a value to a variable. So, what is the actual way?? Thanks for helping me out!

18th Aug 2018, 3:32 AM
Rachit Mehrotra
Rachit Mehrotra - avatar
1 Answer
+ 1
The floating point types store data in their variables in the highest precision possible for them. This precision amount is 7 digits for floats, around 10 for doubles and around 15 for long doubles. So you can try printing 0.2738837382 with setprecision(3) and then setprecision(10) and you will get the correct value later as well.
18th Aug 2018, 10:57 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar