EvilZone
Programming and Scripting => Java => : DreX December 24, 2014, 02:31:06 PM
-
int buyCost=4321;
int sellCost=4252;
int profit=100;
int x=100*profit*sellCost/buyCost;
profit=x/100;
System.out.println (profit);
profit=98 and this is the limit.
The next one (100 into 1000)
int buyCost=4321;
int sellCost=4252;
int profit=1000;
int x=1000*profit*sellCost/buyCost;
profit=x/1000;
System.out.println (profit);
X=-9
And the further up i go the more it misses.
I am trying to get somewhere at least 4+ digits and because I don't know how to make decimal points I was going this way.
-
int buyCost=4321;
int sellCost=4252;
int profit=100;
int x=100*profit*sellCost/buyCost;
profit=x/100;
System.out.println (profit);
profit=98 and this is the limit.
The next one (100 into 1000)
int buyCost=4321;
int sellCost=4252;
int profit=1000;
int x=1000*profit*sellCost/buyCost;
profit=x/1000;
System.out.println (profit);
X=-9
And the further up i go the more it misses.
I am trying to get somewhere at least 4+ digits and because I don't know how to make decimal points I was going this way.
I believe your problem lies in the order of operation. Try putting parentheses around the two integers that are being divided.
-
doesn't work. I assume because he first devides and from that I can only get 1 or 0 and not a decimal %.
-
If you want to stay with your int x, you would need to do this:
public static void main(String[] args) {
int buyCost = 4321;
int sellCost = 4252;
int profit = 1000;
int x = (int) (1000 * profit * (sellCost / (double) buyCost));
profit = x / 1000;
System.out.println(profit);
}
Otherwise you are performing an integer division of this part:
(sellCost / buyCost)
which will result in 0, because decimal places are just cut.
See the output of this:
int buyCost = 4321;
int sellCost = 4252;
int x = sellCost / buyCost;
System.out.println(x);
Result: 0
This even happens if your x is of the type double, because the right part is evaluated first. So it performes an integer division and converts the 0 int to a double afterwards.
int buyCost = 4321;
int sellCost = 4252;
double x = sellCost / buyCost;
System.out.println(x);
Result: 0.0
Only this works as you might expect:
int buyCost = 4321;
int sellCost = 4252;
double x = sellCost / (double) buyCost;
System.out.println(x);
Result: 0.984031474195788
-
If you want to stay with your int x, you would need to do this:
public static void main(String[] args) {
int buyCost = 4321;
int sellCost = 4252;
int profit = 1000;
int x = (int) (1000 * profit * (sellCost / (double) buyCost));
profit = x / 1000;
System.out.println(profit);
}
Otherwise you are performing an integer division of this part:
(sellCost / buyCost)
which will result in 0, because decimal places are just cut.
See the output of this:
int buyCost = 4321;
int sellCost = 4252;
int x = sellCost / buyCost;
System.out.println(x);
Result: 0
This even happens if your x is of the type double, because the right part is evaluated first. So it performes an integer division and converts the 0 int to a double afterwards.
int buyCost = 4321;
int sellCost = 4252;
double x = sellCost / buyCost;
System.out.println(x);
Result: 0.0
Only this works as you might expect:
int buyCost = 4321;
int sellCost = 4252;
double x = sellCost / (double) buyCost;
System.out.println(x);
Result: 0.984031474195788
This works.
Thank you very much.