-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivision.java
More file actions
64 lines (50 loc) · 1.06 KB
/
Copy pathDivision.java
File metadata and controls
64 lines (50 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class Division
{
public static void main(String[] args)
{
System.out.println(divide(10, 2));
System.out.println(divide(100, 11));
System.out.println(divide(1, 4));
System.out.println(divide(5, 2));
System.out.println(divide(5, 3));
System.out.println(divide(1, 12));
System.out.println(divide(1, 13));
System.out.println(divide(1, 14));
}
// a / b
public static String divide(int a, int b)
{
if(a < 0 && b < 0)
{
a = 0 - a;
b = 0 - b;
}
if(b == 0)
return "Invalid";
if(a == 0)
return "0";
int div = 0;
int dec = 0;
while(a-b < 0)
{
a = a * 10;
dec++;
}
int precision = 1000;
a *= precision;
while(a-b >= 0)
{
a = a - b;
div++;
}
//round based off last digit
int last = div - ((int)(div / 10) * 10);
double answer = (((double)div)/(Math.pow(10, dec)*precision));
if(last >= 5)
{
// get 2nd to last digit add that place
answer += (1/((Math.pow(10, dec-1)*precision)));
}
return answer + "";
}
}