Results 1 to 2 of 2
Thread: casting an int
- 09-07-2011, 06:29 PM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 259
- Rep Power
- 11
casting an int
I am trying to take numbers in as an int, then put out the decimal value. for example, 1/2 = 1.2. here is my code:
Java Code:Scanner console = new Scanner(System.in); System.out.print("numerator: "); int numerator = console.nextInt(); System.out.print("denominator: "); int denominator = console.nextInt(); double fraction = (double) (numerator/denominator);
-
Re: casting an int
It's a matter of order. With your parenthesis as they are: (double) (numerator/denominator), you first do int division which always returns an int (here zero), and then change it to a double. Instead place the cast inside the parenthesis:
Java Code:double fraction = ((double) numerator/denominator);
In fact there's no need for the outer parenthesis at all:
Java Code:double fraction = (double) numerator/denominator;
Similar Threads
-
Casting int[][] to int[]
By subith86 in forum New To JavaReplies: 2Last Post: 02-02-2011, 10:48 AM -
Casting
By zzpprk in forum Advanced JavaReplies: 13Last Post: 08-13-2009, 07:59 PM -
What does casting mean?
By sev51 in forum New To JavaReplies: 3Last Post: 01-27-2009, 04:31 PM -
casting help
By soc86 in forum New To JavaReplies: 4Last Post: 01-13-2009, 11:07 PM -
Casting
By leebee in forum New To JavaReplies: 5Last Post: 08-10-2007, 12:24 PM
Bookmarks