+ 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. 😑
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
+ 14
it's okey you did try 😀
+ 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).
+ 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. :>
+ 12
yup thanks , I knew only about iostream , conio , math ..
+ 12
okey but can I use this header file with c ? or just for c++ ?
+ 11
what's iomanip.h ?
+ 11
You'll get explanation and sample code here:
http://www.cplusplus.com/reference/iomanip/
+ 11
yup did it. thanks 😀 😁
+ 10
it will be fine if you have any program to understand
+ 10
let me try 😁
+ 10
You are welcome
+ 9
okey thanks for your suggestion but how can I add precision to them ?
+ 8
i don't know. i never try C before. Maybe @Hatsy Rei know, She is the master.
+ 6
Your answer is correct you can put a tick mark on it
+ 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!
+ 3
most programmers would use a double instead of float these days, it allows the values stored to be more precise.
+ 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?