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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting type double to type int.

Starting out with Java

Converting type double to type int.

by  GT500FOMOCO  Posted    (Edited  )
To convert a [color blue]double[/color] to an [color blue]int[/color] you need to use a cast like this one:

[color blue]int[/color] i = [color red]([/color]int[color red])[/color]d;

Here is a simple Java program that uses a cast to convert a [color blue]double[/color] to an [color blue]int[/color]:

[color blue]public class[/color] DoubleToInt [color red]{[/color]

[color blue]public static void[/color] main[color red]([/color]String[color red][][/color] args[color red]) {[/color]
[color blue]double[/color] d = 10.5;
[color blue]int[/color] i = (int)d;
System.out.println(i);
[color red]}
}[/color]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top