How can I round a number, x, scanned from a file, to i decimal places, scanned from same file in C?
R x i [Round Reals ] Round double value x to i significant decimal places
example: this is scanned from the file: R 3.1415 2
so I need to print out 3.14doing a big loop, has other functio开发者_运维技巧ns so far:
else if (operate == "R") printf("\n");dont know what to put...help?
Try this:
double number;
int places;
fscanf(file, "%lf %d", &number, &places);
printf("%.*f\n", places, number);
printf('%.2f', your_number_var)
will format to two decimal places.
Don't sprintf
your format string like others say, you can do everything in one shot:
printf("%.*f", precision, number)
精彩评论