[fpc-pascal] Formatting of JSON float values

Michael Van Canneyt michael at freepascal.org
Wed Dec 2 19:17:45 CET 2015



On Wed, 2 Dec 2015, Graeme Geldenhuys wrote:

> Hi,
>
> I'm storing the following values in a JSON file using
>
> values:
>  23.67, 23.97, 23.87, 10.0
>
> and stored in a JSON object using code as follows:
>
> procedure TFPReportStreamer.WriteFloat(AName: String; AValue: Extended);
> begin
>  CurrentElement.Add(AName, AValue);
> end;
>
>
> When it is written I see the following:
>
> {
>  "Report": {
>    "Name": "MyReport1",
>    "DateCreated": "20151202T175625",
>    "Pages": {
>      "0": {
>        "Layout": {
>          "val1": 2.36700000000000E+001,
>          "val2": 2.39700000000000E+001,
>          "val3": 2.38700000000000E+001,
>          "val4": 1.00000000000000E+001
>        },
>  ...snip....
> }
>
>
> It's correct, but just curious - is there a way to format the floating
> values and limit them to 4 decimal places? My JSON output should then
> look something like
>
> {
>  "Report": {
>    "Name": "MyReport1",
>    "DateCreated": "20151202T175625",
>    "Pages": {
>      "0": {
>        "Layout": {
>          "val1": 23.6700,
>          "val2": 23.9700,
>          "val3": 23.8700,
>          "val4": 10.0000
>        },
>  ...snip....
> }
>
>
> Python and Java allows this. Can FPC too?

No, currently not. The value is formatted in TFloatNumber.GetAsString.

Michael.



More information about the fpc-pascal mailing list