Re: Mapping Java BigDecimal

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: "Thomas Kellerer" <spam_eater(at)gmx(dot)net>
Cc: <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Mapping Java BigDecimal
Date: 2010-01-19 16:31:10
Message-ID: 4B5589EE020000250002E76C@gw.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Thomas Kellerer <spam_eater(at)gmx(dot)net> wrote:
> Craig Ringer, 19.01.2010 05:37:

>> regress=> insert into test (x) values ('3');
>> INSERT 0 1
>> regress=> insert into test (x) values ('3.0');
>> INSERT 0 1
>> regress=> insert into test (x) values ('3.00');
>> INSERT 0 1
>> regress=> insert into test (x) values ('3.000');
>> INSERT 0 1
>> regress=> select * from test;
>> x
>> -------
>> 3
>> 3.0
>> 3.00
>> 3.000
>> (4 rows)

> My first question: why does anyone pass a numeric value as a
> string ;)

Forget PostgreSQL for just a moment; try this in Java:

import java.math.BigDecimal;
class BigDecimalTests
{
public static void main(String[] args)
{
BigDecimal x;
x = new BigDecimal(3.000);
System.out.println(x.toPlainString());
x = new BigDecimal("3.000");
System.out.println(x.toPlainString());
x = new BigDecimal(1.01);
System.out.println(x.toPlainString());
x = new BigDecimal("1.01");
System.out.println(x.toPlainString());
}
}

For those without Java to play along at home, the result of
compiling and running this are:

3
3.000
1.0100000000000000088817841970012523233890533447265625
1.01

On top of that, in PostgreSQL '3.000' is *not* a character string,
as you are probably assuming. It is treated as type UNKNOWN until
it has to be resolved (similar to the treatment of a NULL literal),
so it can be interpreted as a literal of some other type.

-Kevin

Browse pgsql-jdbc by date

  From Date Subject
Next Message Donald Fraser 2010-01-19 19:18:06 Re: Mapping Java BigDecimal
Previous Message Thomas Kellerer 2010-01-19 16:00:59 Re: Mapping Java BigDecimal