Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

double-->int how to?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I need to cast a double into a int type, lets say by rounding the double into an int:

double dblnum;
int intnum;

intnum = round(dblnum) ???????

Something like this? How can I do it please?

for example if dblnum = 1.6 then intnum = 2, if dblnum = 2.3 then intnum = 2 and so on... this should be easy right?

Tk you
 
Hey dude,
try this piece- The java.lang.Math class is in the standard
API.

Good luck
javaembryo




import java.lang.Math.*;
public class TestDoubleCast {

double dblNumber;
int intNumber;

public static void main (String args[] ) {
TestDoubleCast tdc = new TestDoubleCast();
tdc.dblNumber = 3.6;
System.out.println("Double = " + tdc.dblNumber);
tdc.intNumber = 3;
System.out.println("Int = " + tdc.intNumber);
tdc.intNumber = (int)tdc.dblNumber;
System.out.println("Double converted to int = " + tdc.intNumber);

tdc.intNumber = (int)java.lang.Math.round(tdc.dblNumber);
System.out.println("Double rounded to int = " + tdc.intNumber);

}//main

}//TestDoubleCast
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top