1 #pragma once 2 #ifndef CONVERT_H 3 #define CONVERT_H 4 5 #include <stdio.h> 6 7 /** 8 * sizeof `buf` must be at least 64 bytes 9 */ jbi_ftoa(long double val,char buf[static64],size_t * osz)10static void jbi_ftoa(long double val, char buf[static 64], size_t *osz) { 11 // todo: review 12 int sz = snprintf(buf, 64, "%.8Lf", val); 13 if (sz <= 0) { 14 buf[0] = '\0'; 15 *osz = 0; 16 return; 17 } 18 while (sz > 0 && buf[sz - 1] == '0') { // trim right zeroes 19 buf[sz - 1] = '\0'; 20 sz--; 21 } 22 if ((sz > 0) && (buf[sz - 1] == '.')) { 23 buf[sz - 1] = '\0'; 24 sz--; 25 } 26 *osz = (size_t) sz; 27 } 28 29 #endif 30