File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Doubt in math.round Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in math.round" Watch "Doubt in math.round" New topic
Author

Doubt in math.round

Dhivya rajagopal
Ranch Hand

Joined: Dec 15, 2010
Posts: 42



The following is the output for the above program
Round f4 is -5
Round f5 is 6
Round f6 is -5
Round f7 is 5
My doubt is how -5 became the output for f4?
can you please tell me
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

When a value's decimal part is exactly 0.5, it's rounded up (as specified in the Javadoc comments). For negative numbers this means towards 0. For positive numbers this means away from 0.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Rob Spoor wrote: . . . as specified in the Javadoc comments . . .
It isn’t at all clear in the Javadoc comments. If you find the Java6 version, it says
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:

(long)Math.floor(a + 0.5d)
. . . but the Java7 version says
with ties rounding up.
I think the old version was much easier to understand.
Ifteqar Ald
Ranch Hand

Joined: Dec 26, 2011
Posts: 73
What is the Major difference between Math.Round and Math.Floor Functions?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

saahil gupta wrote:What is the Major difference between Math.Round and Math.Floor Functions?


0 or 1.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

saahil gupta wrote:What is the Major difference between Math.Round and Math.Floor Functions?


When you read the docs for both, what part didn't you understand?

When you executed round() and floor() on 1.0, 1.1, 1.4, 1.5, 1.6, and 1.9, which output did not make sense to you?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11175
    
  16

saahil gupta wrote:What is the Major difference between Math.Round and Math.Floor Functions?

directly from the API:

round:
Returns the closest long to the argument.

Floor:
Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Campbell Ritchie wrote:I think the old version was much easier to understand.

I think the new version is much easier to understand; moreover the old version is decidedly too much information. Documentation should tell me in no uncertain terms what result I will get, to be sure; but it should not lay bare the procedure employed to reach that result.
 
 
subject: Doubt in math.round
 
Similar Threads
Math.round()
a question from Poddar's exam
Math function doubt
Math.round() method
Math.Round