strscan: make it work properly
[tools.git] / math / round.c
1 #include <jh.h>
2 #include <math.h>
3 #include <stdbool.h>
4
5 int main(int argc, char **argv) {
6   if (argc != 2) xperror("invocation: round <target>", 0);
7   double target = strtod(argv[1], NULL);
8   bool intmode = (target == floor(target));
9   char line[128];
10   while (fgets(line, sizeof(line), stdin)) {
11     trim_end(line, "\r\n \t");
12     if (!*line) continue;
13     double n = strtod(line, NULL);
14     double quot = n / target;
15     quot = round(quot);
16     n = quot * target;
17     if (intmode) {
18       printf("%lld\n", (long long)n);
19     } else {
20       printf("%f\n", n);
21     }
22   }
23   return 0;
24 }