Skip to content
Snippets Groups Projects
Commit c9f39ce2 authored by Andrea Censi's avatar Andrea Censi
Browse files

added option for specifying format to JSON double formatting

parent ea3c59b7
No related branches found
No related tags found
No related merge requests found
......@@ -128,7 +128,7 @@ void jo_write_as_matlab(JO jo, FILE*out) {
fprintf(out, "%d", json_object_get_int(jo));
return;
case json_type_double:
case json_type_double:
fprintf(out, "%lg", json_object_get_double(jo));
return;
......
......@@ -357,15 +357,23 @@ int json_object_get_int(struct json_object *this)
}
}
const char *float_format = "%e";
void json_set_float_format(const char*f) {
float_format = f;
}
/* json_object_double */
static int json_object_double_to_json_string(struct json_object* this,
struct printbuf *pb)
{
#define AC_BETTER_PRECISION
#ifdef AC_BETTER_PRECISION
//#warning json: Using better precision in printing floats
if( ((int) this->o.c_double) != this->o.c_double)
return sprintbuf(pb, "%25.18Lg", this->o.c_double);
// return sprintbuf(pb, "%g", this->o.c_double);
return sprintbuf(pb, float_format, this->o.c_double);
else
return sprintbuf(pb, "%d.0", (int) this->o.c_double);
#else
......
......@@ -46,6 +46,9 @@ enum json_type {
/* reference counting functions */
/**
* Increment the reference count of json_object
* @param obj the json_object instance
......@@ -307,4 +310,6 @@ extern struct json_object* json_object_new_string_len(const char *s, int len);
*/
extern char* json_object_get_string(struct json_object *obj);
extern void json_set_float_format(const char*f);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment