Recent

Author Topic: ParambyName - how to string  (Read 627 times)

Nicole

  • Hero Member
  • *****
  • Posts: 1068
ParambyName - how to string
« on: March 26, 2025, 01:44:59 pm »
There is a TEdit, with a Caption of
14,588980
So a string-value

The database awaits a type of
Decimal 5,2
so the allowed entry would be
14.59

Question about "ParambyName....AsFloat"

1)
,
instead of
.
would be a bad idea, right?

There are strictly no formatted stings in any way allowed to use, right?



2)
What about "14,588980".
Would the "as float" convert this to me by itself into 13,59 or will I have to find it by myself.

So can I paste a string value "asFloat" and it works?

3)
Depending on the answer of question 2:
What is the best way to do it?
We have a string, which shall become a double.






Leledumbo

  • Hero Member
  • *****
  • Posts: 8797
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: ParambyName - how to string
« Reply #1 on: March 26, 2025, 02:05:45 pm »
In preference order:
  • Ensure both client and server uses the same locale
  • Modify DefaultFormatSettings to follow server's locale
  • Manual conversion (e.g. FormatFloat)

Zvoni

  • Hero Member
  • *****
  • Posts: 2961
Re: ParambyName - how to string
« Reply #2 on: March 26, 2025, 03:04:49 pm »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3. Uses SysUtils;
  4. Var
  5.   TF:TFormatSettings;
  6.   s:String;
  7.   f:Double;
  8.   b:Boolean;
  9. begin
  10.   TF.DecimalSeparator:=',';
  11.   s:='14,588980';
  12.   b:=TryStrToFloat(s,f,TF);
  13.   Writeln(Round(f*100)/100.0);    
  14.   Readln;
  15. end.

Boils down to:
Code: Pascal  [Select][+][-]
  1. b:=TryStrToFloat(s, f,TF);    
  2. If b Then MyQuery.ParamByName('MyFloatParam').AsFloat:=Round(f*100)/100.0;

In preference order:
  • Ensure both client and server uses the same locale
  • Modify DefaultFormatSettings to follow server's locale
  • Manual conversion (e.g. FormatFloat)
Have to disagree with the first 2 points.
A Float is a Float. Period. As such it will always have the "." as decimal separator for the numeric (float) Data within the Database.
It has nothing to do with a locale of the Server.
Data is Data. A locale chips in, when it comes to DISPLAYING resp. Conversion of the displayed "Value" to the Value the Database understands

The Server might be in the USA, the Client is in Germany.
The only thing of interest is the locale of the Client.
Since the Data in Question resides in a TEdit, it's a String out of the Gates, meaning you need (manual) conversion, in which the client's locale chips in with its Decimal Separator (See my code-snippet above)
« Last Edit: March 27, 2025, 09:15:00 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

tonyw

  • Sr. Member
  • ****
  • Posts: 335
    • MWA Software
Re: ParambyName - how to string
« Reply #3 on: March 27, 2025, 11:13:14 am »
3)
Depending on the answer of question 2:
What is the best way to do it?
We have a string, which shall become a double.
The point to remember is that if you set a numeric parameter as a string then the string is passed to the Firebird server which then attempts to decode it to a numeric value. This will typically use its default local when differentiating between a decimal point or comma.

When you decode that string locally (e.g. using StrToFloat) then your local locale is used to differentiate between a decimal point or comma.

Personally, I would always decode locally. That way it works regardless of where the server is located or how it has been set up (assuming that your local locale is set appropriately).

Nicole

  • Hero Member
  • *****
  • Posts: 1068
[solved] Re: ParambyName - how to string
« Reply #4 on: April 08, 2025, 11:11:10 am »
There is a TEdit, with a Caption of
14,588980
So a string-value

The database awaits a type of
Decimal 5,2
so the allowed entry would be
14.59

Question about "ParambyName....AsFloat"

1)
,
instead of
.
would be a bad idea, right?

There are strictly no formatted stings in any way allowed to use, right?



2)
What about "14,588980".
Would the "as float" convert this to me by itself into 13,59 or will I have to find it by myself.

So can I paste a string value "asFloat" and it works?

3)
Depending on the answer of question 2:
What is the best way to do it?
We have a string, which shall become a double.

Zvoni

  • Hero Member
  • *****
  • Posts: 2961
Re: ParambyName - how to string
« Reply #5 on: April 08, 2025, 11:13:43 am »
Is there a question? :D
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018