round:output without comma for integer targets
[tools.git] / math / round.c
index f87d3c1..f5a07b2 100644 (file)
@@ -1,9 +1,11 @@
 #include <jh.h>
 #include <math.h>
+#include <stdbool.h>
 
 int main(int argc, char **argv) {
   if (argc != 2) xperror("invocation: round <target>", 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");
@@ -12,7 +14,11 @@ int main(int argc, char **argv) {
     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;
 }