[sr-dev] git:master:d5df2e45: core: snexpr.h - custom function to format float value

Daniel-Constantin Mierla miconda at gmail.com
Thu Sep 29 10:06:58 CEST 2022


Module: kamailio
Branch: master
Commit: d5df2e45cf6ae3c8fc61f565c9291b26ff255ede
URL: https://github.com/kamailio/kamailio/commit/d5df2e45cf6ae3c8fc61f565c9291b26ff255ede

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2022-09-29T10:06:03+02:00

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

- use a precision of 4 decimals

---

Modified: src/core/utils/snexpr.h

---

Diff:  https://github.com/kamailio/kamailio/commit/d5df2e45cf6ae3c8fc61f565c9291b26ff255ede.diff
Patch: https://github.com/kamailio/kamailio/commit/d5df2e45cf6ae3c8fc61f565c9291b26ff255ede.patch

---

diff --git a/src/core/utils/snexpr.h b/src/core/utils/snexpr.h
index d1ade48525..d6b9adac86 100644
--- a/src/core/utils/snexpr.h
+++ b/src/core/utils/snexpr.h
@@ -387,6 +387,26 @@ static int to_int(float x)
 }
 
 
+static int snexpr_format_num(char **out, float value)
+{
+	int ret = 0;
+	*out = (char*)malloc(24*sizeof(char));
+	if(*out==NULL) {
+		return -1;
+	}
+	if(value - (long)value != 0) {
+		ret = snprintf(*out, 24, "%.4g", value);
+	} else {
+		ret = snprintf(*out, 24, "%lld", (long long)value);
+	}
+	if((ret < 0) || (ret >= 24)) {
+		free(*out);
+		*out = NULL;
+		return -2;
+	}
+	return 0;
+}
+
 static struct snexpr *snexpr_convert_num(float value, unsigned int ctype)
 {
 	struct snexpr *e = (struct snexpr *)malloc(sizeof(struct snexpr));
@@ -398,7 +418,7 @@ static struct snexpr *snexpr_convert_num(float value, unsigned int ctype)
 	if(ctype == SNE_OP_CONSTSTZ) {
 		e->eflags |= SNEXPR_EXPALLOC | SNEXPR_VALALLOC;
 		e->type = SNE_OP_CONSTSTZ;
-		asprintf(&e->param.stz.sval, "%g", value);
+		snexpr_format_num(&e->param.stz.sval, value);
 		return e;
 	}
 




More information about the sr-dev mailing list