Possible issue with PostgreSQL decimal fields.
Posted: 27.05.2021, 22:00
Hi All!
I'm having some issues when entering floating point types into PostgreSQL. This appears to affect query field editing as well as using parameters for data entry. Here's a small console mode program that gives a quick example. In this case inserting "23.82" into a Decimal(10,6) column causes it to truncate to 23, but trying different values gives a wide range of results (often incorrect ones.) Thanks for any help!
-Mark
I'm having some issues when entering floating point types into PostgreSQL. This appears to affect query field editing as well as using parameters for data entry. Here's a small console mode program that gives a quick example. In this case inserting "23.82" into a Decimal(10,6) column causes it to truncate to 23, but trying different values gives a wide range of results (often incorrect ones.) Thanks for any help!
-Mark
Code: Select all
program ZeosPgDecimalTest;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, System.Classes,
Windows,
Data.DB,
ZDbcIntfs, ZConnection, ZDataset;
var
ZConn: TZConnection;
ZQuery: TZQuery;
begin
try
ZConn := TZConnection.Create(nil);
ZConn.Protocol := 'postgresql';
ZConn.User := 'mark';
ZConn.Password := 'zeosRox';
ZConn.HostName := 'myserver';
ZConn.Database := 'testdb';
ZConn.LibraryLocation := 'C:\U\DbClients\Postgres\32bit\libpq.dll';
ZConn.Connect;
if ZConn.Connected then
Writeln('Connected');
ZConn.ExecuteDirect('create table if not exists dectest (id bigint primary key, mydecimal decimal(10,6))');
ZConn.ExecuteDirect('delete from dectest');
ZQuery := TZQuery.Create(nil);
ZQuery.Connection := ZConn;
// Note that we have to double the :: on := to escape it from the parser.
ZQuery.SQL.Text := 'select * from dectest';
ZQuery.Open;
ZQuery.Insert;
ZQuery.Fields[0].Value := 1;
// It doesn't seem to matter how we enter it...
// ZQuery.Fields[1].Value := 23.82; // truncates to 23
// ZQuery.Fields[1].Value := '23.82'; // truncates to 23
ZQuery.Fields[1].AsFloat := 23.82; // truncates to 23
Writeln('Input value: 23.82');
ZQuery.Post;
ZQuery.Close;
ZQuery.SQL.Text := 'select mydecimal from dectest where id = 1';
ZQuery.Open;
while not ZQuery.Eof do
begin
Writeln('Output value: ' + ZQuery.Fields[0].AsString);
ZQuery.Next;
end;
Writeln('Done');
ZConn.Disconnect;
ZQuery.Free;
ZConn.Free;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Write('Press Enter to quit...');
ReadLn;
end.