Limit precision of double in json object or array

rwel59
Posts: 97
Joined: Thu Oct 12, 2017 3:32 pm

Limit precision of double in json object or array

Postby rwel59 » Mon Dec 04, 2017 11:41 pm

When I add a double to json array or object, I get 6 decimal places - right side is loaded with zeros: 1.100000.

Tried using CJSON_CreateNumber and CJSON_CreateDouble but can't figure out a way to set precision. Is there a method available?

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Limit precision of double in json object or array

Postby tele_player » Tue Dec 05, 2017 7:44 am

It seems you are confusing the precision of a data type with its printed representation.

rwel59
Posts: 97
Joined: Thu Oct 12, 2017 3:32 pm

Re: Limit precision of double in json object or array

Postby rwel59 » Tue Dec 05, 2017 1:56 pm

Yes, my description was incorrect but I had already shutdown when I posted the issue last night. To clarify the question, is there a method to strip zeros from the data representation?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Limit precision of double in json object or array

Postby kolban » Sun Mar 11, 2018 5:11 am

Would it be possible to elaborate on the question? I'm not following the notion of what it means to add a "double" to an "array" or "object".
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

rwel59
Posts: 97
Joined: Thu Oct 12, 2017 3:32 pm

Re: Limit precision of double in json object or array

Postby rwel59 » Sun Mar 11, 2018 10:18 pm

Neil,
This is taking me back awhile... I was originally sending a json data array of doubles and wanted to limit the precision - the precision was not the issue but my json ended up with a bunch of zeros following the 1 decimal point.

I just took a quick look at the code and at some point I switched from using json data array to a json object which is where I am still at and the json is formed without trailing zeros.

A background routine I was using could not handle the data array so that is why I made the switch - whether this is what solved the trailing zeros issue or not, I do not remember.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Limit precision of double in json object or array

Postby kolban » Sun Mar 11, 2018 10:26 pm

Interesting ... I am sensing correctly, the JSON you were transmitting may have shown up as:

[ 2.000000000000000000000 ]

as opposed to

[ 2.0 ]

as you might have hoped/expected?

Semantically 2.0000000000000000 == 2.0 so it was in fact "working" but was perhaps redundant in its data usage. I think we would have to look at how the JSON was being generated and what (if any) library was being used.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

rwel59
Posts: 97
Joined: Thu Oct 12, 2017 3:32 pm

Re: Limit precision of double in json object or array

Postby rwel59 » Sun Mar 11, 2018 10:59 pm

that is correct. my json looked like [2.100000000, 2.100000000] as opposed to [2.1, 2.1].

I was using the snippets JSON.cpp to build the json and form the string.

Who is online

Users browsing this forum: No registered users and 88 guests