Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 01E6E635393 for ; Mon, 18 Jan 2010 05:10:13 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 46972-01-8 for ; Mon, 18 Jan 2010 09:10:02 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.b2bcentrum.cz (dns.b2bcentrum.cz [62.77.97.39]) by mail.postgresql.org (Postfix) with ESMTP id 03B8063561D for ; Mon, 18 Jan 2010 04:57:00 -0400 (AST) Received: from localhost (dns [127.0.0.1]) by mail.b2bcentrum.cz (Postfix) with ESMTP id ECE401850003; Mon, 18 Jan 2010 09:56:57 +0100 (CET) X-Virus-Scanned: amavisd-new at b2bcentrum.cz Received: from mail.b2bcentrum.cz ([127.0.0.1]) by localhost (dns.b2bcentrum.cz [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id f-a0rmC4X-TD; Mon, 18 Jan 2010 09:56:56 +0100 (CET) Received: from [192.168.1.77] (unknown [77.242.93.97]) (Authenticated sender: bednar) by mail.b2bcentrum.cz (Postfix) with ESMTP id 251551850002; Mon, 18 Jan 2010 09:56:56 +0100 (CET) Message-ID: <4B542257.1040505@b2bcentrum.cz> Date: Mon, 18 Jan 2010 09:56:55 +0100 From: =?UTF-8?B?SmFrdWIgQmVkbsOhxZk=?= User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: Craig Ringer CC: pgsql-jdbc@postgresql.org Subject: Re: Mapping Java BigDecimal References: <4B540B7D.1080306@b2bcentrum.cz> <4B541C40.9040206@postnewspapers.com.au> In-Reply-To: <4B541C40.9040206@postnewspapers.com.au> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201001/40 X-Sequence-Number: 20236 Craig Ringer wrote: > Jakub Bednář wrote: > >> Hi All, >> >> We decide add support PostgreSQL database (now supporting only Oracle >> database) to our product. >> >> In Oracle we mapping Java BigDecimal to number(19, 2), in PostgreSQL to >> numeric(19, 2). >> >> If I store to "BigDecimal column" number without decimal, e.g. "3", than >> Oracle JDBC driver return "3", but PostgreSQL JDBC driver return "3.00". >> > > You're retrieving a number with two digits of precision, so it's giving > that to you. > > >> Is there some way (mapping, server setup, jdbc driver setup,...) how >> reach return number without trailing zeroes on decimal position? >> > > If you map the column in Pg as "numeric" without scale and precision > then it'll store whatever scale and precision you give it. If you map it > in Pg with a specified scale and precision, input values will be treated > as being of that scale and precision. It sounds like Oracle instead > treats those as *limits* for values and preserves the input scale and > precision even if they're specified for the column type. > > I don't know whether Oracle or Pg are more "correct" here - you're > giving Pg "3" so arguably it shouldn't assume "3.00" and should in fact > return "3". OTOH, you've told it what the scale and precision are for > the column, and inputs to the column should be presumed to fit that > scale and precision. > > You should probably just strip the trailing zeroes for display when you > format your numbers for the current locale, and retain the internal > representation however it is. Alternately, store 'numeric' in Pg to > retain the input scale and precision. > > regress=> create table test (x numeric, y numeric(19,2)); > CREATE TABLE > regress=> insert into test VALUES ('3', '3'); > INSERT 0 1 > regress=> select * from test; > x | y > ---+------ > 3 | 3.00 > (1 row) > > > ... and if you want, use a CHECK constraint to limit it. You could wrap > that up in a domain type if you like. I tend to create a domain anyway > for my NUMERIC types so I don't have to repeat the scale and precision > all over the place, and so the name of the type more accurately reflects > its use (eg currency types, etc). > > -- > Craig Ringer > Thanks Craig. JB