Sign in Join

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

Mailing lists / lazarus / 2011-11

JSON float saving scientific value

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

silvioprog

Hi,

I'm trying to save a float value: 1.5. But some class is converting the value to "1.50000000000000E+000" (please see:
http://imagebin.org/182341).

How to I save the value exactly as 1.5?

My project test in attached.

Thx.

Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> Facebook - <facebook.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * =============================================== -------------- next part -------------- A non-text attachment was scrubbed... Name: json_test.zip Type: application/zip Size: 2305 bytes Desc: not available URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20111103/2854e1fe/attachment.zip>

Reply by email

tcoq

Hello silvio,
1.500000000E+000 is exactly 1.5, from a numerical point of view. It seems that you are getting what you need: storing a real. Why would you want to have the "1.5" string? What is the issue? Thierry

----- Mail Original -----
De: "silvioprog"
?: "Lazarus mailing list"
Envoy?: Vendredi 4 Novembre 2011 00h24:50 GMT +01:00 Amsterdam / Berlin / Berne / Rome / Stockholm / Vienne Objet: [Lazarus] JSON float saving scientific value

Hi,

I'm trying to save a float value: 1.5. But some class is converting the value to "1.50000000000000E+000" (please see:
http://imagebin.org/182341).

How to I save the value exactly as 1.5?

My project test in attached.

Thx.

Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> Facebook - <facebook.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * =============================================== --

Reply by email

silvioprog
to tcoq

2011/11/4 tcoq :

Hello silvio, 1.500000000E+000 is exactly 1.5, from a numerical point of view. It seems that you are getting what you need: storing a real. Why would you want to have the "1.5" string? What is the issue? Thierry

For transporting data to a database from a mobile, via WAP. I think I'll use string "1.5", because using scientific notation can grow my final JSON file.

Thanks! :)

Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> Facebook - <facebook.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email

tcoq

Hello,
either you recode the function below:

"function TJSONFloatNumber.GetAsString: TJSONStringType;
begin
  Str(FValue,Result);
end;
"

Either by changing to using formatted string conversion, using FloatToStrF function. or
by implementing your new type yourself, something like "TJSNExtFloatNumber" which overrides the GetAsString function.

The second way should work without changing the JSON base code. However loading a JSON stream will still get you the base TJSONFloatNumber.

I hope this helps.
Thierry

----- Mail Original -----
De: "silvioprog"
?: "Lazarus mailing list"
Envoy?: Vendredi 4 Novembre 2011 00h24:50 GMT +01:00 Amsterdam / Berlin / Berne / Rome / Stockholm / Vienne Objet: [Lazarus] JSON float saving scientific value

Hi,

I'm trying to save a float value: 1.5. But some class is converting the value to "1.50000000000000E+000" (please see:
http://imagebin.org/182341).

How to I save the value exactly as 1.5?

My project test in attached.

Thx.

Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> Facebook - <facebook.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * =============================================== --

Reply by email

Michael Van Canneyt
to tcoq
tcoq wrote 10 lines
Hello, either you recode the function below: "function TJSONFloatNumber.GetAsString: TJSONStringType; begin Str(FValue,Result); end; " Either by changing to using formatted string conversion, using FloatToStrF function.

You should definitely not do that, because then the resulting string is localized. JSON is not localized. It MUST use a . as the decimal separator.

or by implementing your new type yourself, something like "TJSNExtFloatNumber" which overrides the GetAsString function. The second way should work without changing the JSON base code. However loading a JSON stream will still get you the base TJSONFloatNumber.

The best way probably is to extend FormatJSON to accept a new option and use that when returning JSON data.

Michael.

Reply by email

tcoq
to Michael Van Canneyt

Indeed, the localization is an issue, but it is possible to force FloatToStr to output a decimal '.' in all cases, whatever the localization. Isn't it DecimalSeparator? Change DecimalSeparator to '.'.
Something like this (untested/uncompiled pseudo-code):

Function FloatToStrMine( aNumber: Double): String;
var
  oldDecimal: Char;
begin
  oldDecimal := DecimalSeparator;
  DecimalSeparator := '.';
  try
    result := FloatToStr( aNumber)
  finally
    DecimalSeparator := oldDecimal;
  end;
end;

Best regards,
Thierry

----- Mail Original -----
De: "Michael Van Canneyt"
?: "Lazarus mailing list"
Envoy?: Dimanche 6 Novembre 2011 16h22:09 GMT +01:00 Amsterdam / Berlin / Berne / Rome / Stockholm / Vienne Objet: Re: [Lazarus] JSON float saving scientific value ...
You should definitely not do that, because then the resulting string is localized. JSON is not localized. It MUST use a . as the decimal separator.

Reply by email

silvioprog
to tcoq: either you recode the function below: "function TJSONFloatNumber.GetAsString:…

2011/11/6 tcoq :
[...]

or by implementing your new type yourself, something like "TJSNExtFloatNumber" which overrides the GetAsString function. The second way should work without changing the JSON base code. However loading a JSON stream will still get you the base TJSONFloatNumber.

Yeah. I think this part will solve my problem. Thanks! :)

Silvio Cl?cio =============================================== Blog - <silvioprog.com.br> Twitter - <twitter.com/silvioprog> Facebook - <facebook.com/silvioprog> LazSolutions - <code.google.com/p/lazsolutions> Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR> =============================================== ? ?* Conhe?a nosso canal IRC sobre Lazarus: #lazarus-br * ===============================================

Reply by email