add precision
authorLaurent Mazet <mazet@softndesign.org>
Mon, 2 Jan 2023 22:29:22 +0000 (23:29 +0100)
committerLaurent Mazet <mazet@softndesign.org>
Mon, 2 Jan 2023 22:29:22 +0000 (23:29 +0100)
calc.c
fdprintf.c

diff --git a/calc.c b/calc.c
index e255bd2ca421baf948ee8400f8685859555d1960..95eef4ed89131f5e954e2f4d199cbf82177da7fe 100644 (file)
--- a/calc.c
+++ b/calc.c
@@ -26,6 +26,7 @@
 /* gobal variables */
 
 char *progname = NULL;
+int precision = 6;
 
 /* help function */
 
@@ -34,6 +35,7 @@ int usage (int ret)
     int fd = ret ? stdfderr : stdfdout;
     fdprintf (fd, "usage: %s\n", progname);
     fdprintf (fd, " -h : help message\n");
+    fdprintf (fd, " -p : precision (%d)\n", precision);
     fdprintf (fd, " -v : verbose level (%d)\n", verbose);
 
     return ret;
@@ -70,6 +72,14 @@ int main (int argc, char *argv[])
         }
         char c = arg[1];
         switch (c) {
+        case 'p':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                PRINTERR ("%s: missing precision\n", progname);
+                return usage (1);
+            }
+            precision = atoi (arg);
+            break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -83,6 +93,10 @@ int main (int argc, char *argv[])
             return usage (c != 'h');
         }
     }
+    
+    /* format */
+    char format[8] = "=> %.f\n";
+    format[4] = '0' + precision;
 
     /* read from input stream */
 
@@ -104,7 +118,7 @@ int main (int argc, char *argv[])
                     ret = 1;
                        } else {
                     VERBOSE (INFO, print_element (element, 0));
-                    PRINTOUT ("=> %f\n", evaluate_element (element, 0));
+                    PRINTOUT (format, evaluate_element (element, 0));
                     delelement (element);
                     ret = 0;
                 }
@@ -174,5 +188,6 @@ int main (int argc, char *argv[])
 // test: echo "2 + * 3" | calc.exe | grep -q 'error'
 // test: echo "sqrt 2" | calc.exe | grep -q 'error'
 // test: echo "pow (2)" | calc.exe | grep -q 'error'
+// test: echo "1.23456789" | calc.exe -p 3 | grep -q '1\.234'
 
 /* vim: set ts=4 sw=4 et: */
index 76722fc508851be20d29be71f906a06b6f3e276b..17b847368ee41061498e1a85169a4e166f9453cf 100644 (file)
@@ -137,13 +137,14 @@ int fdprintf (int fd, const char *fmt, ...)
                     *str++ = '-';
                     f = -f;
                 }
-                t = getexponant (&f, -6);
+                if (sz == 0) sz = 6;
+                t = getexponant (&f, -sz);
                 u = (int)f;
                 str = itoa (str, u, 0);
-                d = (int)((f - u) * tenpower (6));
+                d = (int)((f - u) * tenpower (sz));
                 if (d > 0) {
                     *str++ = '.';
-                    str = itoa (str, d, 6);
+                    str = itoa (str, d, sz);
                 }
                 while (*(str - 1) == '0') {
                     str--;