[fpc-pascal] Formatting of JSON float values
    Graeme Geldenhuys 
    mailinglists at geldenhuys.co.uk
       
    Wed Dec  2 19:02:25 CET 2015
    
    
  
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
    
    
More information about the fpc-pascal
mailing list