Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Math.floor with negative numbers

 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there....
System.out.println(Math.floor(-2.1));
what do you think the answer is?
-3.0 that's what.
i thought it rounded towards zero...i guess it does with positive numbers, but the point is that .floor gets the lowest whole number... so therefore it must be -3.0 ... silly little things like that must be watched for on the exam eh.
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct.
Think about 'floor', which is always lower than you, regardless negative or positive.
 
Abbas Alafoo
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a funny thing about Math.ceil:
Math.ceil(-0.5) is -0.0 and not 0.0 !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic