The Artima Developer Community
Sponsored Link

Java Answers Forum
Discrepancy in Rounding the numbers

1 reply on 1 page. Most recent reply: Aug 16, 2006 12:29 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Venkatesan

Posts: 9
Nickname: venkki
Registered: Jun, 2003

Discrepancy in Rounding the numbers Posted: Aug 15, 2006 11:38 AM
Reply to this message Reply
Advertisement
Hi,

Why Java and Oracle have a different approach in rounding the numbers with decimals?

In Java,
Math.round(1.5) gives the output as 2
Math.round(-1.5) gives the output as -1

In Oracle,
select round(1.5) from dual gives the output as 2
select round(-1.5) from dual gives the output as 2

If you look the outputs in both the platforms, positive rounding works fine as expected. But not the negative rounding.

This leads to produce mismatch reports which dealing with numbers and calculations.


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Discrepancy in Rounding the numbers Posted: Aug 16, 2006 12:29 AM
Reply to this message Reply
> In Java,
> Math.round(1.5) gives the output as 2
> Math.round(-1.5) gives the output as -1
>
> In Oracle,
> select round(1.5) from dual gives the output as 2
> select round(-1.5) from dual gives the output as 2
>
> If you look the outputs in both the platforms, positive
> rounding works fine as expected. But not the negative
> rounding.

Neither is inconsistant. Java consistantly rounds up (i.e. more positive), whilst Oracle consistantly rounds away from zero. Both are equally correct. Rounding to the nearest even value is also a valid option (used most frequently in financial systems).

From a pure mathmatical viewpoint there is nothing to choose between rounding 1.5 up or down to the nearest integer. Therefore, there is no 'correct' way and thus no reason to say that one works 'as expected' and one doesn't.

V.

Flat View: This topic has 1 reply on 1 page
Topic: Rookie question on Boolean operators Previous Topic   Next Topic Topic: Handling files efficiently

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use