Adopt use of gnulib for portability.
[pspp-builds.git] / src / data-out.c
index 6439b607045f18e5566deec8a438b9197f88d03e..2600ccb8037dfac35af8494b60b6c5b23a6eaa9f 100644 (file)
@@ -34,6 +34,9 @@
 #include "str.h"
 #include "var.h"
 
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
 #include "debug-print.h"
 \f
 /* Public functions. */
@@ -193,10 +196,7 @@ data_out (char *s, const struct fmt_spec *fp, const union value *v)
 void
 num_to_string (double v, char *s, int w, int d)
 {
-  struct fmt_spec f;
-  f.type = FMT_F;
-  f.w = w;
-  f.d = d;
+  struct fmt_spec f = make_output_format (FMT_F, w, d);
   convert_F (s, &f, v);
 }
 \f
@@ -1045,7 +1045,7 @@ try_F (char *dst, const struct fmt_spec *fp, double number)
               /* There are no decimal places, so there's no way
                  that the value can be shortened.  Either it fits
                  or it doesn't. */
-              char buf[40];
+              char buf[41];
               sprintf (buf, "%*.0f", fp->w, number);
               if (strlen (buf) <= fp->w) 
                 {
@@ -1119,6 +1119,7 @@ format_and_round (char *dst, double number, const struct fmt_spec *fp,
 
   sprintf (buf, "%.*f", decimals, number);
 
+  /* Omit integer part if it's 0. */
   if (!memcmp (buf, "0.", 2))
     memmove (buf, buf + 1, strlen (buf));
   else if (!memcmp (buf, "-0.", 3))
@@ -1218,6 +1219,10 @@ format_and_round (char *dst, double number, const struct fmt_spec *fp,
         }
     }
 
+  /* Omit `-' if value output is zero. */
+  if (buf[0] == '-' && buf[strspn (buf, "-.0")] == '\0')
+    memmove (buf, buf + 1, strlen (buf));
+
   buf_copy_str_lpad (dst, fp->w, buf);
   return 1;
 }