X-Git-Url: http://git.thejh.net/?p=tools.git;a=blobdiff_plain;f=math%2Fround.c;h=f5a07b28dae5b6229144e649b6ebff8982084d88;hp=e422fd0ce216147856f5ed2ddc3ee4871e1f85c2;hb=384ee75e98daccfb078ee084edfdfa0b3188b79e;hpb=7ebc44a8d838298394178687975cf9d2994ff8be diff --git a/math/round.c b/math/round.c index e422fd0..f5a07b2 100644 --- a/math/round.c +++ b/math/round.c @@ -1,16 +1,24 @@ #include #include +#include int main(int argc, char **argv) { if (argc != 2) xperror("invocation: round ", 0); double target = strtod(argv[1], NULL); + bool intmode = (target == floor(target)); char line[128]; while (fgets(line, sizeof(line), stdin)) { + trim_end(line, "\r\n \t"); + if (!*line) continue; double n = strtod(line, NULL); double quot = n / target; quot = round(quot); n = quot * target; - printf("%f\n", n); + if (intmode) { + printf("%lld\n", (long long)n); + } else { + printf("%f\n", n); + } } return 0; }