aboutsummaryrefslogtreecommitdiffstats
path: root/src/stdlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/stdlib.c')
-rw-r--r--src/stdlib.c40
1 files changed, 39 insertions, 1 deletions
diff --git a/src/stdlib.c b/src/stdlib.c
index b7b11dc..4714e93 100644
--- a/src/stdlib.c
+++ b/src/stdlib.c
@@ -23,7 +23,6 @@
#include <stdlib.h>
#include <ctype.h>
-#include <heap.h>
#include <stdarg.h>
#include <string.h>
@@ -134,3 +133,42 @@ int atoi(const char *s)
return (neg == 0) ? n : -n;
}
+
+char *ftostr(char *buf, float f)
+{
+ if (buf == 0)
+ return 0;
+
+ unsigned int i = 0; // offset
+
+ // strip decimals, convert in reverse
+ for (int d = f; d != 0; d /= 10)
+ buf[i++] = d % 10 + '0';
+
+ // reverse
+ for (unsigned int j = 0; j < i / 2; j++) {
+ char c = buf[i - j - 1];
+ buf[i - j - 1] = buf[j];
+ buf[j] = c;
+ }
+
+ if ((float)((int)f) == f)
+ goto end;
+
+ buf[i++] = '.';
+
+ // decimals
+ float d = f;
+ // precision of 5 is safe, more gets questionable
+ for (unsigned int p = 0; p < 5; p++) {
+ d *= 10;
+ buf[i++] = (int)d % 10 + '0';
+ }
+
+ // trim 0's
+ for (unsigned int j = i - 1; buf[j] == '0'; j--)
+ buf[j] = '\0';
+end:
+ buf[i] = '\0';
+ return buf;
+}