• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Changing a double to an int

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
========================
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]


 
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
I'm so happy! And I wish to make this tiny ad happy too:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic