int jsonp_dtostr(char *buffer, size_t size, double value, int precision) { int ret; char *start, *end; size_t length; if (precision == 0) precision = 17; ret = snprintf(buffer, size, "%.*g", precision, value); if(ret < 0) return -1; length = (size_t)ret; if(length >= size) return -1; #if JSON_HAVE_LOCALECONV from_locale(buffer); #endif /* Make sure there's a dot or 'e' in the output. Otherwise a real is converted to an integer when decoding */ if(strchr(buffer, '.') == NULL && strchr(buffer, 'e') == NULL) { if(length + 3 >= size) { /* No space to append ".0" */ return -1; } buffer[length] = '.'; buffer[length + 1] = '0'; buffer[length + 2] = '\0'; length += 2; } /* Remove leading '+' from positive exponent. Also remove leading zeros from exponents (added by some printf() implementations) */ start = strchr(buffer, 'e'); if(start) { start++; end = start + 1; if(*start == '-') start++; while(*end == '0') end++; if(end != start) { memmove(start, end, length - (size_t)(end - buffer)); length -= (size_t)(end - start); } } return (int)length; }
int os_dtostr(double value, char *dst, size_t size) { int ret; char *start, *end; size_t length; ret = snprintf(dst, size, "%.17g", value); if(ret < 0) return -1; length = (size_t)ret; if(length >= size) return -1; from_locale(dst); /* Make sure there's a dot or 'e' in the output. Otherwise a real is converted to an integer when decoding */ if(strchr(dst, '.') == NULL && strchr(dst, 'e') == NULL) { if(length + 3 >= size) { /* No space to append ".0" */ return -1; } dst[length] = '.'; dst[length + 1] = '0'; dst[length + 2] = '\0'; length += 2; } /* Remove leading '+' from positive exponent. Also remove leading zeros from exponents (added by some printf() implementations) */ start = strchr(dst, 'e'); if(start) { start++; end = start + 1; if(*start == '-') start++; while(*end == '0') end++; if(end != start) { memmove(start, end, length - (size_t)(end - dst)); length -= (size_t)(end - start); } } return (int)length; }