Results 1 to 7 of 7
- 03-26-2012, 03:05 PM #1
how to calculate division in java
hi,
Java Code:double a=1/2; System.out.println(a);
how to make it 0.5?
regards
dhilip
- 03-26-2012, 03:16 PM #2
Re: how to calculate division in java
1 and 2 are both ints. An int divided by an int will give you an int. Variables that are ints do not have decimal places.
You can cast one or both of the values to a double before doing the division, or you could use double literals (1.0 instead of 1, for example).How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 03-26-2012, 03:18 PM #3
Re: how to calculate division in java
in your code you are doing a integer division and then automatic type promotion is performed.So u will always get 1/2 as 0.
Use it like this:
float a = 1f; float b = 2f;
float c = a/b;
*you can use double instead of float
- 03-26-2012, 03:19 PM #4
Re: how to calculate division in java
- 03-26-2012, 03:20 PM #5
Re: how to calculate division in java
- 09-07-2016, 09:51 AM #6
Member
- Join Date
- Sep 2016
- Posts
- 1
- Rep Power
- 0
Re: how to calculate division in java
package com.corejava.sonu;
import java.util.Scanner;
public class Division {
public static void main(String args[]){
float x,y,z;
System.out.println("Enter the two integers value");
Scanner in =new Scanner(System.in);
x=in.nextInt();
y=in.nextInt();
z=x/y;
System.out.println("Division of two integers="+z);
}
}
**try this simple example for division using float
- 09-07-2016, 12:27 PM #7
Re: how to calculate division in java
"It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013
Similar Threads
-
I think this is Java that has Division ..
By quidd in forum New To JavaReplies: 1Last Post: 01-20-2012, 07:31 AM -
Help with java division
By louisw in forum Advanced JavaReplies: 6Last Post: 09-29-2011, 07:09 AM -
I code a binary division in java, but the result is not desirable.
By amityadav9314 in forum New To JavaReplies: 1Last Post: 03-09-2011, 04:45 PM -
java division and decimal error
By heartysnowy in forum New To JavaReplies: 5Last Post: 10-07-2009, 05:57 PM -
Calculate Tax in java
By toby in forum New To JavaReplies: 2Last Post: 07-30-2007, 10:03 AM
Bookmarks