PR#394: don't always append the ".0" if the double value rounds to zero because some custom formats *will* include it (e.g. %.2f).

Also try to accomodate formats to explicitly exclude the decimal (e.g. %.0f).
This commit is contained in:
Eric Haszlakiewicz
2017-12-24 13:45:52 -05:00
parent eb55c83600
commit 8270e83552
3 changed files with 69 additions and 29 deletions

View File

@@ -54,4 +54,24 @@ int main()
printf("obj.to_string(back to default format)=%s\n", json_object_to_json_string(obj));
json_object_put(obj);
obj = json_object_new_double(12.0);
printf("obj(12.0).to_string(default format)=%s\n", json_object_to_json_string(obj));
if (json_c_set_serialization_double_format("%.0f", JSON_C_OPTION_GLOBAL) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
printf("obj(12.0).to_string(%%.0f)=%s\n", json_object_to_json_string(obj));
if (json_c_set_serialization_double_format("%.0g", JSON_C_OPTION_GLOBAL) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
printf("obj(12.0).to_string(%%.0g)=%s\n", json_object_to_json_string(obj));
if (json_c_set_serialization_double_format("%.2g", JSON_C_OPTION_GLOBAL) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
printf("obj(12.0).to_string(%%.1g)=%s\n", json_object_to_json_string(obj));
// Reset to default to free memory
if (json_c_set_serialization_double_format(NULL, JSON_C_OPTION_GLOBAL) < 0)
printf("ERROR: json_c_set_serialization_double_format() failed");
json_object_put(obj);
}

View File

@@ -12,3 +12,7 @@ obj.to_string(with thread format)=T0.52X
obj.to_string(long thread format)=Ttttttttttttt0.52xxxxxxxxxxxxxxxxxxX
obj.to_string(back to global format)=x0.524y
obj.to_string(back to default format)=0.52381
obj(12.0).to_string(default format)=12.0
obj(12.0).to_string(%.0f)=12
obj(12.0).to_string(%.0g)=1e+01
obj(12.0).to_string(%.1g)=12.0