what is wrong with Math.ceil

W

wheresjim

I think I am going crazy. I have used Math.ceil many times to round up
a number, and for some reason I cannot make it work properly today.
The following code:

public class MathTest
{
public static void main(String args[])
{
System.out.println(Math.ceil(40/15));
}
}

Produces this output:

2.0

Should it not take the result (2.6666666666666666666666666666667
according to my Microsoft calculator) and round it up to 3?
 
C

Carl Howells

wheresjim said:
I think I am going crazy. I have used Math.ceil many times to round up
a number, and for some reason I cannot make it work properly today.
The following code:

public class MathTest
{
public static void main(String args[])
{
System.out.println(Math.ceil(40/15));
}
}

Produces this output:

2.0

Should it not take the result (2.6666666666666666666666666666667
according to my Microsoft calculator) and round it up to 3?

You sure that's the result? Try this code?

public class MathTest
{
public static void main(String [] args)
{
System.out.println(40/15);
}
}

You might feel kind of silly after running that code...
 
A

Ann

wheresjim said:
I think I am going crazy. I have used Math.ceil many times to round up
a number, and for some reason I cannot make it work properly today.
The following code:

public class MathTest
{
public static void main(String args[])
{
System.out.println(Math.ceil(40/15));
}
}

Produces this output:

2.0

Should it not take the result (2.6666666666666666666666666666667
according to my Microsoft calculator) and round it up to 3?
First thing is 40/15 -> 2
Then ceil(2) = 2.0
 
T

The Abrasive Sponge

wheresjim said:
I think I am going crazy. I have used Math.ceil many times to round up
a number, and for some reason I cannot make it work properly today.
The following code:

public class MathTest
{
public static void main(String args[])
{
System.out.println(Math.ceil(40/15));
}
}

Produces this output:

2.0

Should it not take the result (2.6666666666666666666666666666667
according to my Microsoft calculator) and round it up to 3?


A division of integer renders an integer so
40/15 = 2

:)

and the ceil of 2 is 2.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top