Omit trailing zeros when not necessary
This commit is contained in:
parent
9eb2967340
commit
792deba5f3
30
ell.c
30
ell.c
|
@ -888,18 +888,18 @@ execute (struct context *ctx, struct item *body, struct item **result) {
|
||||||
#define defn(name) static bool name \
|
#define defn(name) static bool name \
|
||||||
(struct context *ctx, struct item *args, struct item **result)
|
(struct context *ctx, struct item *args, struct item **result)
|
||||||
|
|
||||||
static struct item *new_format (const char *fmt, ...) ATTRIBUTE_PRINTF (1, 2);
|
|
||||||
|
|
||||||
static struct item *
|
static struct item *
|
||||||
new_format (const char *fmt, ...) {
|
new_number (double n) {
|
||||||
va_list ap;
|
char *s;
|
||||||
va_start (ap, fmt);
|
if (!(s = format ("%f", n)))
|
||||||
char *s = vformat (fmt, ap);
|
|
||||||
va_end (ap);
|
|
||||||
|
|
||||||
if (!s)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
char *p = strchr (s, 0);
|
||||||
|
while (--p > s && *p == '0')
|
||||||
|
*p = 0;
|
||||||
|
if (*p == '.')
|
||||||
|
*p = 0;
|
||||||
|
|
||||||
struct item *item = new_string (s, strlen (s));
|
struct item *item = new_string (s, strlen (s));
|
||||||
free (s);
|
free (s);
|
||||||
return item;
|
return item;
|
||||||
|
@ -1090,7 +1090,7 @@ defn (fn_system) {
|
||||||
return set_error (ctx, "first argument must be string");
|
return set_error (ctx, "first argument must be string");
|
||||||
if (command->next)
|
if (command->next)
|
||||||
return set_error (ctx, "cannot deal with multiple arguments");
|
return set_error (ctx, "cannot deal with multiple arguments");
|
||||||
return check (ctx, (*result = new_format ("%d", system (command->value))));
|
return check (ctx, (*result = new_number (system (command->value))));
|
||||||
}
|
}
|
||||||
|
|
||||||
defn (fn_plus) {
|
defn (fn_plus) {
|
||||||
|
@ -1100,17 +1100,17 @@ defn (fn_plus) {
|
||||||
return set_error (ctx, "arguments must be strings");
|
return set_error (ctx, "arguments must be strings");
|
||||||
res += strtod (args->value, NULL);
|
res += strtod (args->value, NULL);
|
||||||
}
|
}
|
||||||
return check (ctx, (*result = new_format ("%f", res)));
|
return check (ctx, (*result = new_number (res)));
|
||||||
}
|
}
|
||||||
|
|
||||||
defn (fn_minus) {
|
defn (fn_minus) {
|
||||||
double res = -0.0;
|
double res = 0.0;
|
||||||
for (; args; args = args->next) {
|
for (; args; args = args->next) {
|
||||||
if (args->type != ITEM_STRING)
|
if (args->type != ITEM_STRING)
|
||||||
return set_error (ctx, "arguments must be strings");
|
return set_error (ctx, "arguments must be strings");
|
||||||
res -= strtod (args->value, NULL);
|
res -= strtod (args->value, NULL);
|
||||||
}
|
}
|
||||||
return check (ctx, (*result = new_format ("%f", res)));
|
return check (ctx, (*result = new_number (res)));
|
||||||
}
|
}
|
||||||
|
|
||||||
defn (fn_multiply) {
|
defn (fn_multiply) {
|
||||||
|
@ -1120,7 +1120,7 @@ defn (fn_multiply) {
|
||||||
return set_error (ctx, "arguments must be strings");
|
return set_error (ctx, "arguments must be strings");
|
||||||
res *= strtod (args->value, NULL);
|
res *= strtod (args->value, NULL);
|
||||||
}
|
}
|
||||||
return check (ctx, (*result = new_format ("%f", res)));
|
return check (ctx, (*result = new_number (res)));
|
||||||
}
|
}
|
||||||
|
|
||||||
defn (fn_divide) {
|
defn (fn_divide) {
|
||||||
|
@ -1134,7 +1134,7 @@ defn (fn_divide) {
|
||||||
return set_error (ctx, "division by zero");
|
return set_error (ctx, "division by zero");
|
||||||
res /= x;
|
res /= x;
|
||||||
}
|
}
|
||||||
return check (ctx, (*result = new_format ("%f", res)));
|
return check (ctx, (*result = new_number (res)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
Loading…
Reference in New Issue