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.

Mailing lists / lazarus / 2011-11
7 messages · 3 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
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.
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.
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! :)
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.
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.
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.
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! :)