aspose file tools*
The moose likes Java in General and the fly likes Discrepancy in Rounding the numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Discrepancy in Rounding the numbers " Watch "Discrepancy in Rounding the numbers " New topic
Author

Discrepancy in Rounding the numbers

Venkatesan Ramalingam
Greenhorn

Joined: Feb 24, 2004
Posts: 6
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 we 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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Well, probably because SQL and Java were specified by different people. There is no generally accepted procedure for rounding in mathematics...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Don't know. I googled for rounding, and found a couple of discussions. Here, and here. There doesn't seem to be a consistent definition of rounding.

But at least round 2.5->3 and 3.5->4 is consistent. In which case, you round up on halves. So -1.5 rounds UP to -1. Yes, -1 is more than -1.5. Which is similar to what it says in the API specification for java.lang.Math.

CR
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Perhaps Oracle/SQL uses the odd/even rule: http://ts.nist.gov/ts/htdocs/230/235/vtmpresent/5pretest/tsld027.htm
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
In general, I would think that if you care about the exact results of functions, with exact parameters, then you should not be using floating-point (float, double).

Any floating-point number, represented in a computer, is potentially inexact. Some numbers can be represented exactly, but it depends upon the precise details of the floating-point representation used. You should try to avoid making your code depend on that.

If you care about exact numbers, use integer arithmetic (maybe with implicit scaling, for fixed decimal places) or maybe BigDecimal.

This is particularly crucial for financial calculations. With the possible exception of some complicated derivatives, on which I am unqualified to comment, financial calculations should never be done in floating point.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Discrepancy in Rounding the numbers
 
Similar Threads
Math class
Math.round( -.5)?
Rounding Doubles to Two Decimal Places
bigger value in function MAX(x,y)
Math.round() onK&B