Skip site navigation (1) Skip section navigation (2)

Bad BigDecimal

From: "Jason Orendorff" <jason(at)jorendorff(dot)com>
To: <pgsql-interfaces(at)postgresql(dot)org>
Subject: Bad BigDecimal
Date: 2001-08-18 03:55:13
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-interfacespgsql-jdbc
Reply-To: sender

Hi.  I'm having a problem with the JDBC driver.  It's excellent for
the most part - kudos to Mr. Mount - and this is the first problem
I've run into.  I think there's a bug; read below for my explanation
and (I think) the fix.

The problem:  When I call getBigDecimal() on a ResultSet, it
sometimes throws an exception:

  Bad BigDecimal 174.50
    at org.postgresql.jdbc2.ResultSet.getBigDecimal(
    at org.postgresql.jdbc2.ResultSet.getBigDecimal(
    ...blah blah blah...
  org.postgresql.util.PSQLException: Bad BigDecimal 174.50

I think the problem is on line 984 of
org/postgresql/jdbc2/  Here's the method I'm looking at
(slightly reformatted to fit in this e-mail message):

  public java.math.BigDecimal getBigDecimal(int columnIndex)
      throws SQLException
      // Now must call BigDecimal with a scale otherwise JBuilder
      // barfs
      return getBigDecimal(columnIndex,0);

Notice that it's calling getBigDecimal(int, int) with a 0 for the
second argument.  Here's the source of that method (again, slightly

  public BigDecimal getBigDecimal(int columnIndex, int scale)
      throws SQLException
      String s = getFixedString(columnIndex);
      BigDecimal val;

      if (s != null)

              val = new BigDecimal(s);
            } catch (NumberFormatException e) {
              throw new PSQLException("postgresql.res.badbigdec",s);
          if (scale==-1) return val;
              return val.setScale(scale);
            } catch (ArithmeticException e) {
              throw new PSQLException("postgresql.res.badbigdec",s);
      return null;              // SQL NULL

Okay.  The fact that the default scale is 0 causes essentially
the following to happen:
  BigDecimal val;
  val = new BigDecimal("174.50");
  return val.setScale(0);

This causes an exception because 174.50 can't be exactly represented
by a BigDecimal that has scale 0.

The fix:  change the appropriate line to read:
  return getBigDecimal(columnIndex, -1);

I'm sure I'm probably overlooking something; please let me know...
Also, please copy me on replies!  Thanks!

Thanks in advance,
Jason Orendorff
freelance programmer


pgsql-interfaces by date

Next:From: Killian MayDate: 2001-08-18 19:11:13
Subject: mdopen error
Previous:From: Dave PageDate: 2001-08-17 19:51:45
Subject: RE: [ODBC] RE: ODBC merge module for Microsoft Installer

pgsql-jdbc by date

Next:From: Peter RogersDate: 2001-08-19 21:51:29
Subject: Re: Unable to connect to database
Previous:From: Peter RogersDate: 2001-08-17 20:26:40
Subject: Unable to connect to database

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group