开发者

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.14

doing 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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜