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

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

AW: Slightly OT: Rounding Negative Numbers

From: Stefan Jahnke <Stefan.Jahnke_at_bov.de>
Date: Thu, 29 Aug 2002 01:33:31 -0800
Message-ID: <F001.004C241F.20020829013331@fatcity.com>


I think it is correct to round -0.875 to 0.87, since what you do is multiply a by 100 (-87.5).
If you round this, the correct value is -87. Divided by 100 = -0.87. So, Java is fine.

Stefan Jahnke
Consultant
BOV Aktiengesellschaft
Voice: +49 201 - 4513-298
Fax: +49 201 - 4513-149
mailto:stefan.jahnke_at_bov.de

visit our website: http://www.bov.de
subscribe to our newsletter: http://www.bov.de/presse/newsletter.asp

Behalten Sie den Ueberblick - mit dem neuen BasicOverView, unserer Seminaruebersicht fuer das 2. Halbjahr 2002. Sie haben noch kein Exemplar? Schreiben Sie eine E-Mail an mailto:qualification_at_bov.de oder rufen Sie uns an unter 0 18 03 / 73 64 62 73!

Wie Sie wissen, koennen ueber das Internet versandte E-Mails leicht unter fremden Namen erstellt oder manipuliert werden. Aus diesem Grunde bitten wir um Verstaendnis dafuer, dass wir zu Ihrem und unserem Schutz die rechtliche Verbindlichkeit der vorstehenden Erklaerungen und Aeusserungen ausschliessen.

As you are probably aware, e-mails sent via the Internet can easily be copied or manipulated by third parties. For this reason we would ask for your understanding that, for your own protection and ours, we must decline all legal responsibility for the validity of the statements and comments given above.

-----Ursprüngliche Nachricht-----
Von: Khedr, Waleed [mailto:Waleed.Khedr_at_FMR.COM] Gesendet: Mittwoch, 28. August 2002 22:19 An: Multiple recipients of list ORACLE-L Betreff: RE: Slightly OT: Rounding Negative Numbers

The way Java calculates round is (int)Math.floor(a + 0.5f)

So you could enforce your rounding algorithm this way:

import java.*;
public class Round {

         public static void main(String[] args) {
                 double a = 0.875;
                 int i_rounded;
                 if (a >= 0) i_rounded = (int)java.lang.Math.round(a*100);
                     else    i_rounded = (int)java.lang.Math.floor(a*100 -
0.5);
                 double z = i_rounded;
                 z  = z / 100;
                 System.out.println(z);
                 }
         }


Regards,

Waleed

-----Original Message-----
Sent: Wednesday, August 28, 2002 11:59 AM To: Multiple recipients of list ORACLE-L

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: Khedr, Waleed
  INET: Waleed.Khedr_at_FMR.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: Stefan Jahnke
  INET: Stefan.Jahnke_at_bov.de

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 Thu Aug 29 2002 - 04:33:31 CDT

Original text of this message

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