Description

ARDUINOJSON_USE_DOUBLE determines the type used to store floating point values in JsonVariant:

  • If ARDUINOJSON_USE_DOUBLE == 0, then JsonVariant stores a float
  • If ARDUINOJSON_USE_DOUBLE == 1, then JsonVariant stores a double

The default is 0 on embedded systems, 1 otherwise.

Breaks the ArduinoJson Assistant

The ArduinoJson Assistant assumes that you use the default settings.

If you change this value, the capacity required to store your document will change, and the results given by the ArduinoJson Assistant won’t match.

Several .ino or .cpp files?

Be careful if several compilation units compose your program, i.e., if your project contains several .ino or .cpp files.

You should define the same value of ARDUINOJSON_USE_DOUBLE in each compilation unit; otherwise, the executable will be much bigger because it will contain two variants of the library.

Examples

Example with 32-bit precision

The following program uses 32-bit precision:

#define ARDUINOJSON_USE_DOUBLE 0
#include <ArduinoJson.h>

void example_float()
{
  StaticJsonDocument<200> doc;
  doc["pi"] = 3.14159265359;
  serializeJson(doc, Serial);
}

It produces the following output:

{"pi":3.141593}

Example with 64-bit precision

The following program uses 64-bit precision:

#define ARDUINOJSON_USE_DOUBLE 1
#include <ArduinoJson.h>

void example_double()
{
  StaticJsonDocument<200> doc;
  doc["pi"] = 3.14159265359;
  serializeJson(doc, Serial);
}

It produces the following output:

{"pi":3.141592654}

See also