Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gDHHR-0001UN-3n for pgsql-general@arkaria.postgresql.org; Thu, 18 Oct 2018 23:00:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gDHFJ-0002Mb-Vl for pgsql-general@arkaria.postgresql.org; Thu, 18 Oct 2018 22:58:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gDHFJ-0002MU-Nb for pgsql-general@lists.postgresql.org; Thu, 18 Oct 2018 22:58:25 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gDHFD-0004YZ-B7 for pgsql-general@lists.postgresql.org; Thu, 18 Oct 2018 22:58:25 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w9IMwEfE030661; Thu, 18 Oct 2018 18:58:14 -0400 From: Tom Lane To: "Peter J. Holzer" cc: pgsql-general@lists.postgresql.org Subject: Re: convert real to numeric. In-reply-to: <20181018211328.jg373dpwc3dczhyo@hjp.at> References: <23387.1539872140@sss.pgh.pa.us> <20181018211328.jg373dpwc3dczhyo@hjp.at> Comments: In-reply-to "Peter J. Holzer" message dated "Thu, 18 Oct 2018 23:13:28 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <30659.1539903493.1@sss.pgh.pa.us> Date: Thu, 18 Oct 2018 18:58:13 -0400 Message-ID: <30660.1539903493@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "Peter J. Holzer" writes: > On 2018-10-18 10:15:40 -0400, Tom Lane wrote: >> You could ju-jitsu the system into duplicating that behavior by casting >> to text (which invokes float4out) and then to numeric: > I suggest casting first to float8 and then to numeric. The conversion > from float4 to float8 is exact, and any rounding error introduced by the > float8->numeric conversion is certainly much smaller than the > uncertainty of the original float4 value. I do not think that the OP will care for the results of that. The problem is that now the output function will think that the result is worth printing to 16 digits, and the last ten or so of those will be garbage. As an example, even though the cited value happens to work nicely: regression=# select '17637.75'::float4::float8; float8 ---------- 17637.75 (1 row) nearby ones don't: regression=# select '17637.74'::float4::float8; float8 ----------------- 17637.740234375 (1 row) Yeah, in some sense that's a valid representation of the stored float4, but it likely has little to do with the originally presented value. regards, tom lane