Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-12

Formatting of JSON float values

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

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?

If this was a database, I would normally convert the floats to a integer value and truncate what I don't need (eg: currency amounts). Then when read back, I would divide the integer value again to get to the original float value. But I don't really want to do this with JSON.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp
Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 54 lines
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.

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote
No, currently not. The value is formatted in TFloatNumber.GetAsString.

Ok, thanks.

Second question. What happens to floating points with different locales and decimal separators? eg: USA and UK uses a dot (.) as decimal separator, but [odd] countries like South Africa use a comma (,) - would this affect FPC's JSON read/write?

Or does the JSON specification define the decimal separator to always be a dot?

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp
Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 12 lines
On 2015-12-02 18:17, Michael Van Canneyt wrote: > No, currently not. The value is formatted in TFloatNumber.GetAsString. Ok, thanks. Second question. What happens to floating points with different locales and decimal separators? eg: USA and UK uses a dot (.) as decimal separator, but [odd] countries like South Africa use a comma (,) - would this affect FPC's JSON read/write? Or does the JSON specification define the decimal separator to always be a dot?

The decimal separator is always a dot.

The value is formatter using str from the system unit, and disregards any locale.

Michael.