GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Changing a double to an int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Changing a double to an int" Watch "Changing a double to an int" New topic
Author

Changing a double to an int

ione walker
Ranch Hand

Joined: Apr 19, 2001
Posts: 31
Hi,
Thank You all for the help that you give.
I have a variable that I will call OccourancePerLine.
This value is calculated as a double.
1 / How can I truncate it to a integer?
ie OccourancePerLine = 5.6
I want IntOccourancePerLine = 5
2 / How can I get the value rounded to an Integer?
ie OccourancePerLine = 5.6
I want IntOccourancePerLine = 6

------------------
=======================
Ione Walker
walkeri@uas.net
========================


=======================<BR>Ione Walker<BR>walkeri@usa.net<BR>========================
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Ione.
To truncate, simply do a cast:

To round, use the java.lang.Math class:

Here, you need to cast because if you input a double to Math.round(), the result is a long, and to fit it into an int, you have to downcast.
Art
ione walker
Ranch Hand

Joined: Apr 19, 2001
Posts: 31
Yet again you guys & gals
are a HUGE help.

Originally posted by Art Metzer:
[B]Hi, Ione.
To truncate, simply do a cast:

To round, use the java.lang.Math class:

Here, you need to cast because if you input a double to Math.round(), the result is a long, and to fit it into an int, you have to downcast.
Art[/B]

Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
OccurancePerLine sounds familiar... you can also calculate the value as an int from the start:
int occurancePerLine = someString.length % lineLength;
that will give you the whole number integer value that relates to how many times the "string" will fit on the line.
[This message has been edited by Greg Harris (edited May 04, 2001).]


what?
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Folks,
Just thought that you all could alternatively use
the <code> Math.ceil(5.6) </code> to covert the double to int.
Cheers,
Ravindra Mohan
 
GeeCON Prague 2014
 
subject: Changing a double to an int