Omit trailing zeros when not necessary

This commit is contained in:
Přemysl Eric Janouch 2017-05-21 10:33:04 +02:00
parent 9eb2967340
commit 792deba5f3
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 15 additions and 15 deletions

30
ell.c
View File

@ -888,18 +888,18 @@ execute (struct context *ctx, struct item *body, struct item **result) {
#define defn(name) static bool name \
(struct context *ctx, struct item *args, struct item **result)
static struct item *new_format (const char *fmt, ...) ATTRIBUTE_PRINTF (1, 2);
static struct item *
new_format (const char *fmt, ...) {
va_list ap;
va_start (ap, fmt);
char *s = vformat (fmt, ap);
va_end (ap);
if (!s)
new_number (double n) {
char *s;
if (!(s = format ("%f", n)))
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));
free (s);
return item;
@ -1090,7 +1090,7 @@ defn (fn_system) {
return set_error (ctx, "first argument must be string");
if (command->next)
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) {
@ -1100,17 +1100,17 @@ defn (fn_plus) {
return set_error (ctx, "arguments must be strings");
res += strtod (args->value, NULL);
}
return check (ctx, (*result = new_format ("%f", res)));
return check (ctx, (*result = new_number (res)));
}
defn (fn_minus) {
double res = -0.0;
double res = 0.0;
for (; args; args = args->next) {
if (args->type != ITEM_STRING)
return set_error (ctx, "arguments must be strings");
res -= strtod (args->value, NULL);
}
return check (ctx, (*result = new_format ("%f", res)));
return check (ctx, (*result = new_number (res)));
}
defn (fn_multiply) {
@ -1120,7 +1120,7 @@ defn (fn_multiply) {
return set_error (ctx, "arguments must be strings");
res *= strtod (args->value, NULL);
}
return check (ctx, (*result = new_format ("%f", res)));
return check (ctx, (*result = new_number (res)));
}
defn (fn_divide) {
@ -1134,7 +1134,7 @@ defn (fn_divide) {
return set_error (ctx, "division by zero");
res /= x;
}
return check (ctx, (*result = new_format ("%f", res)));
return check (ctx, (*result = new_number (res)));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -