Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> Re: Slightly OT: Rounding Negative Numbers

Re: Slightly OT: Rounding Negative Numbers

From: Ron Rogers <RROGERS_at_galottery.org>
Date: Wed, 28 Aug 2002 10:13:24 -0800
Message-ID: <F001.004C1B94.20020828101324@fatcity.com>


Bill,
 I haven't tried it with negative numbers but I found round to create error where commission dollars are concerned. I use the trunc function to give the results in two decimal places where the extra half cent makes a difference over the period of time. total = trunc(sum * %,2)
Select trunc(-.872,2) from dual;
87
Ron
ROR mª¿ªm

>>> wbuchan_at_uk.intasys.com 08/28/02 11:58AM >>>

I'm trying to consolidate some numbers generated by a PL/SQL report vs. a
Java based report and these seem to be out due to rounding errors on negative numbers.

In PL/SQL if I for example, round to 2 decimal places:

SQL> select round(-0.875,2) from dual;

ROUND(-0.875,2)


            -.88

In Java:

import java.*;

public class Round {

         public static void main(String[] args) {
                 double a = -0.875;
                 int i = (int)java.lang.Math.round(a*100);
                 double z = i;
                 z  = z / 100;
                 System.out.println(z);
                 }
         }

>java Round

-0.87

which is 0.01 different (1 pence in this case). If I use positive numbers
(+0.875) when the answer is 0.88 for both PL/SQL and Java. PL/SQL's answer
looks "more"correct to me for the negative value - but what is the mathematically correct rounding of -0.875?!

Thanks!

-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com 
-- 
Author: Bill Buchan
  INET: wbuchan_at_uk.intasys.com 

Fat City Network Services    -- (858) 538-5051  FAX: (858) 538-5051
San Diego, California        -- Public Internet access / Mailing Lists
--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).
-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: Ron Rogers
  INET: RROGERS_at_galottery.org

Fat City Network Services    -- (858) 538-5051  FAX: (858) 538-5051
San Diego, California        -- Public Internet access / Mailing Lists
--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).
Received on Wed Aug 28 2002 - 13:13:24 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US