5 Replies - 12799 Views - Last Post: 15 March 2010 - 06:31 PM Rate Topic: -----

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • Posts: 1,688
  • Joined: 13-March 10

Difference between Math.round() and Math.rint()

Posted 15 March 2010 - 05:14 PM

Hey, just as the topic title indicates i want to know the difference between Math.rint() and Math.round()
as as far as i know they do the same thing.. Any feedback would be helpful. Thx in advance
Is This A Good Question/Topic? 1
  • +

Replies To: Difference between Math.round() and Math.rint()

#2 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Difference between Math.round() and Math.rint()

Posted 15 March 2010 - 05:48 PM

Math.rint(double) returns a double value closest to the argument that is passed to it and equal to the mathematical integer. Math.round(double) returns a long value closest to the value passed to it
This code demonstrates the thing
class RoundRint 
{
	public static void main(String[] args) 
	{
		double val = 424.122262;
	
		System.out.println(Math.rint(val));
		System.out.println(Math.round(val));
	}
}


The output is

Quote

C:\Users\Prasad\Desktop>java RoundRint
424.0
424

C:\Users\Prasad\Desktop>

Hope this helps
Happy Coding
:)

This post has been edited by pdkharkar: 15 March 2010 - 06:16 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Difference between Math.round() and Math.rint()

Posted 15 March 2010 - 06:05 PM

You cannot always
double x = Math.round(val);

because if wont work if val is ouside int range like if val = 1.000E10 or 1.000E-10
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Difference between Math.round() and Math.rint()

Posted 15 March 2010 - 06:16 PM

View Postpdkharkar, on 15 March 2010 - 08:48 PM, said:

Math.random(double) returns a long value closest to the value passed to it


Don't you mean Math.round()? :)
Was This Post Helpful? 1
  • +
  • -

#5 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Difference between Math.round() and Math.rint()

Posted 15 March 2010 - 06:18 PM

View Postmacosxnerd101, on 16 March 2010 - 05:46 AM, said:

View Postpdkharkar, on 15 March 2010 - 08:48 PM, said:

Math.random(double) returns a long value closest to the value passed to it


Don't you mean Math.round()? :)

@macosxnerd101
Extremely sorry sir
and thanks for correcting
I could have totally confused darek9576
Thanks again
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10468
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Difference between Math.round() and Math.rint()

Posted 15 March 2010 - 06:31 PM

Not a problem. I think he knew what you meant. It was just a little typo. :)

Edit: It double posted for some reason.

This post has been edited by macosxnerd101: 15 March 2010 - 06:31 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1