jsoncpp icon indicating copy to clipboard operation
jsoncpp copied to clipboard

Float to JSON with specific format

Open skldfm opened this issue 3 years ago • 1 comments

I have a JSON object struct and there is only one specific float type member which I have to export to JSON with exactly one decimal digit after the comma ...

C like formatting "example": printf("%.1f", floatValue)

Is there any direct or indirect way to do this? (I mean with(in) jsoncpp feature set :) )

skldfm avatar Apr 04 '22 10:04 skldfm

Yes, you have control of related settings when you configure a writer builder.

Note there's no comma. JSON has a defined format with no locale, so it's going to be a "." as the decimal separator.

https://github.com/open-source-parsers/jsoncpp/blob/master/src/lib_json/json_writer.cpp#L1162

  (*settings)["precision"] = 17;
  (*settings)["precisionType"] = "significant";

The "precisionType" setting can be "significant" or "decimal".

  • "significant" requests a "%.*g" format (this is the default)

  • "decimal" requests a "%.*f" format.

BillyDonahue avatar Apr 04 '22 14:04 BillyDonahue