How can i add precision to float numbers in c and c++ ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 13

How can i add precision to float numbers in c and c++ ?

I am creating a program for maths I stuck at rounding off and precision with c. 😑

29th Apr 2017, 3:02 PM
Jay Tailor
Jay Tailor - avatar
18 Answers
+ 16
Do you mean like this : #include <iostream> #include <iomanip> using namespace std; int main() { float x = 1.0/3; cout<<setprecision(2)<<x; return 0; } //output is 0.33
29th Apr 2017, 3:46 PM
Agus Mei
Agus Mei - avatar
+ 14
it's okey you did try 😀
29th Apr 2017, 4:28 PM
Jay Tailor
Jay Tailor - avatar
+ 14
I found this answer by Google search.. it's for c language. 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).
3rd May 2017, 12:47 AM
Jay Tailor
Jay Tailor - avatar
+ 13
I don't think <iomanip> is available for C. I did a lookup and adjusting float precision on C seems to be a whole other story. http://stackoverflow.com/questions/9213340/how-to-set-precision-of-a-float The idea is to round the value off using printf() built-in syntaxes. P.S. The master is actually @Ace, not me. :>
29th Apr 2017, 4:22 PM
Hatsy Rei
Hatsy Rei - avatar
+ 12
yup thanks , I knew only about iostream , conio , math ..
29th Apr 2017, 3:56 PM
Jay Tailor
Jay Tailor - avatar
+ 12
okey but can I use this header file with c ? or just for c++ ?
29th Apr 2017, 3:58 PM
Jay Tailor
Jay Tailor - avatar
+ 11
what's iomanip.h ?
29th Apr 2017, 3:48 PM
Jay Tailor
Jay Tailor - avatar
+ 11
You'll get explanation and sample code here: http://www.cplusplus.com/reference/iomanip/
29th Apr 2017, 3:54 PM
Agus Mei
Agus Mei - avatar
+ 11
yup did it. thanks 😀 😁
6th May 2017, 3:46 AM
Jay Tailor
Jay Tailor - avatar
+ 10
it will be fine if you have any program to understand
29th Apr 2017, 3:36 PM
Jay Tailor
Jay Tailor - avatar
+ 10
let me try 😁
29th Apr 2017, 3:47 PM
Jay Tailor
Jay Tailor - avatar
+ 10
You are welcome
29th Apr 2017, 3:58 PM
Agus Mei
Agus Mei - avatar
+ 9
okey thanks for your suggestion but how can I add precision to them ?
29th Apr 2017, 3:28 PM
Jay Tailor
Jay Tailor - avatar
+ 8
i don't know. i never try C before. Maybe @Hatsy Rei know, She is the master.
29th Apr 2017, 4:02 PM
Agus Mei
Agus Mei - avatar
+ 6
Your answer is correct you can put a tick mark on it
6th May 2017, 3:41 AM
Vaibhav Sharma
Vaibhav Sharma - avatar
+ 4
it depends on just how precise you need to be, a double will use 8 bytes to store the numbers where a float will only use 4 bytes. Make sure you are being very aware of floats and integers as any division of an integer that has a remainder will be truncated and cause havoc!
29th Apr 2017, 3:34 PM
Jason Hoffman
Jason Hoffman - avatar
+ 3
most programmers would use a double instead of float these days, it allows the values stored to be more precise.
29th Apr 2017, 3:27 PM
Jason Hoffman
Jason Hoffman - avatar
+ 1
Wasn't it something like printf ("Number: &3.2f",variable); to have a 000.00 number so if you have something like 342.98754 it will only show 342.987? Or is this something different?
30th Apr 2017, 8:53 AM
Vodolle
Vodolle - avatar