• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Math.round() ? please explain ?

 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone explain what is the difference between these two ?
System.out.println(Math.round(3.5)); //4
System.out.println(Math.round(-3.5)); //-3
why what is the logic behind this ?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically:
Java Math.round() which by the way returns long always leans towards higher number in case of those in the midle like : 2.5, 3.5, -2.5, -3.5.
so if you have -5.5 first highest integer/non-decimal is -5.
if you have 6.5 it takes first highest integer which is 7.
Tarik
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

...Java Math.round() which by the way returns long...


This is not always the case. There are two 'round' methods:
int <- Math.round(float)
long <- Math.round(double)
Sylvia
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi mark,
i read in Val's note that,
Math.round(n) = Math.floor(n+0.5);
check with ur example. its right.
regards
maulin
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic