From 7eb572702ca012a89370cc94a297eeb346a8dee9 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 5 Apr 2018 11:25:18 -0400 Subject: font on flash, ftostr --- src/stdlib.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'src/stdlib.c') 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 #include -#include #include #include @@ -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; +} -- cgit v1.2.3