Browse code

core: snexpr.h - custom function to format float value

- use a precision of 4 decimals

Daniel-Constantin Mierla authored on 29/09/2022 08:06:03
Showing 1 changed files
... ...
@@ -387,6 +387,26 @@ static int to_int(float x)
387 387
 }
388 388
 
389 389
 
390
+static int snexpr_format_num(char **out, float value)
391
+{
392
+	int ret = 0;
393
+	*out = (char*)malloc(24*sizeof(char));
394
+	if(*out==NULL) {
395
+		return -1;
396
+	}
397
+	if(value - (long)value != 0) {
398
+		ret = snprintf(*out, 24, "%.4g", value);
399
+	} else {
400
+		ret = snprintf(*out, 24, "%lld", (long long)value);
401
+	}
402
+	if((ret < 0) || (ret >= 24)) {
403
+		free(*out);
404
+		*out = NULL;
405
+		return -2;
406
+	}
407
+	return 0;
408
+}
409
+
390 410
 static struct snexpr *snexpr_convert_num(float value, unsigned int ctype)
391 411
 {
392 412
 	struct snexpr *e = (struct snexpr *)malloc(sizeof(struct snexpr));
... ...
@@ -398,7 +418,7 @@ static struct snexpr *snexpr_convert_num(float value, unsigned int ctype)
398 418
 	if(ctype == SNE_OP_CONSTSTZ) {
399 419
 		e->eflags |= SNEXPR_EXPALLOC | SNEXPR_VALALLOC;
400 420
 		e->type = SNE_OP_CONSTSTZ;
401
-		asprintf(&e->param.stz.sval, "%g", value);
421
+		snexpr_format_num(&e->param.stz.sval, value);
402 422
 		return e;
403 423
 	}
404 424