Java percent of number [duplicate] - java

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
Is there any way to calculate (for example) 50% of 120?
I tried:
int k = (int)(120 / 100)*50;
But it doesn't work.

int k = (int)(120 / 100)*50;
The above does not work because you are performing an
integer division expression (120 / 100) which result is
integer 1, and then multiplying that result to 50, giving
the final result of 50.
If you want to calculate 50% of 120, use:
int k = (int)(120*(50.0f/100.0f));
more generally:
int k = (int)(value*(percentage/100.0f));

int k = (int)(120*50.0/100.0);

Never use floating point primitive types if you want exact numbers and consistent results, instead use BigDecimal.
The problem with your code is that result of (120/100) is 1, since 120/100=1.2 in reality, but as per java, int/int is always an int.
To solve your question for now, cast either value to a float or double and cast result back to int.

I suggest using BigDecimal, rather than float or double. Division by 100 is always exact in BigDecimal, but can cause rounding error in float or double.
That means that, for example, using BigDecimal 50% of x plus 30% of x plus 20% of x will always sum to exactly x.

it is simple as 2 * 2 = 4 :)
int k = (int)(50 * 120) / 100;

Division must be float, not int
(120f * 50 / 100f)

You don't need floating point in this case you can write
int i = 120 * 50 / 100;
or
int i = 120 / 2;

Related

Division operation is giving me the wrong result [duplicate]

This question already has answers here:
Why does integer division code give the wrong answer? [duplicate]
(4 answers)
Closed last year.
I'm trying to divide one number by another in Processing, and I keep getting the wrong answer.
float a;
a = 3/2;
println(a);
gives me
1.0
when then answer should be 1.5.
Trying a different number,
float a;
a = 2/3;
println(a);
gives me
0.0
when the answer should be 0.666666....
Is there something I'm missing? I looked over the Processing documentation for division and it seems like I'm using the right syntax.
Like others have said, you're using integer division.
Remember that int values can't hold decimal places. So if you do a calculation that would result in decimal places, those values are truncated and you're left with only the whole number part (in other words, the integer part).
int x = 1;
int y = 2;
int z = x/y; //0
int x = 5;
int y = 2;
int z = x/y; //2
You're using int literal values (digits like 2 and 3), and those don't have decimal places, so Processing treats them like int values, and they obey the above rules.
If you care about the decimal places, then either store them in float or double variables:
float x = 1;
float y = 2;
float z = x/y; //.5
float x = 5;
float y = 2;
float z = x/y; //2.5
Or just use float literals by adding a decimal part:
float a = 2.0/3.0;
Processing now knows that these are float values, so you'll keep the decimal places.
Perhaps Processing is interpreting both 3 and 2 as integers. And, since you are dividing one integer by another, it is giving you integer division.
Try changing one or both to 3.0 and/or 2.0 instead.
Your division in interpreted as an integer division and will most probably follow Java integer division rules (since Processing seem to be based on Java).
Java inherits most of its division rules from C, these being explained in more details in a specific question/answer here.

Get the next higher integer value in java [duplicate]

This question already has answers here:
How to round up the result of integer division?
(18 answers)
How to round up integer division and have int result in Java? [duplicate]
(9 answers)
Closed 4 years ago.
I know I can use Math.java functions to get the floor, ceil or round value for a double or float, but my question is -- Is it possible to always get the higher integer value if a decimal point comes in my value
For example
int chunkSize = 91 / 8 ;
which will be equal to 11.375
If I apply floor, ceil or round to this number it will return 11 I want 12.
Simply If I have 11.xxx I need to get 12 , if I have 50.xxx I want 51
Sorry The chunkSize should be int
How can I achieve this?
Math.ceil() will do the work.
But, your assumption that 91 / 8 is 11.375 is wrong.
In java, integer division returns the integer value of the division (11 in your case).
In order to get the float value, you need to cast (or add .0) to one of the arguments:
float chunkSize = 91 / 8.0 ;
Math.ceil(chunkSize); // will return 12!
ceil is supposed to do just that. I quote:
Returns the smallest (closest to negative infinity) double value that
is greater than or equal to the argument and is equal to a
mathematical integer.
EDIT: (thanks to Peter Lawrey): taking another look at your code you have another problem. You store the result of integer division in a float variable: float chunkSize = 91 / 8 ; java looks at the arguments of the division and as they are both integers it performs integer division thus the result is again an integer (the result of the division rounded down). Now even if you assign this to the float chunkSize it will still be an integer(missing the double part) and ceil, round and floor will all return the same value. To avoid that add a .0 to 91: float chunkSize = 91.0 / 8;. This makes one of the arguments double precision and thus double division will be performed, returning a double result.
If you want to do integer division rounding up you can do
x / y rounded up, assuming x and y are positive
long div = (x + y - 1) / y;
In your case
(91 + 8 - 1) / 8 = 98 / 8 = 12
This works because you want to round up (add one) for every value except an exact multiple (which is why you add y - 1)
Firstly, when you write float chunkSize = 91 / 8 ; it will print 11.0 instead of 11.375
because both 91 and 8 are int type values. For the result to be decimal value, either dividend or divisor
or both have to be decimal value type. So, float chunkSize = 91.0 / 8; will result 11.375 and
now you can apply Math.ceil() to get the upper value. Math.ceil(chunkSize); will result in 12.0

Why this equation returns zero even though it should not? [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 9 years ago.
So, I'm doing a fighting game in java and I have this equation that always returns zero.
int x = 90/100*300;
It should be 270 but it returns zero. :|
You're doing integer calculation, so 90/100 results in 0.
If you write it 90.0/100*300, the calculations will be done with doubles (then you'll need to cast it back to int if you want).
The problem here is it first divides 90/100 which is actually 0.9 however since it is an int type the value is 0 and then when you multiply 0 with 300 the output is 0.
90/100 is 0 remainder 90.
You can fix this by re-arranging the values
int x = 90 * 300 / 100; // 270
Do the multiplications before the divisions and as long as you don't get an overflow, this will be more accurate.
Java in this case carries out the multiplication/division in order.
It also interpretes each number as integers.
So, 90/100 = 0.9 and it gets truncated to 0.
and 0 * 300 = 0.
So you end up with 0
You use integer datatype you use float or double datatype and get proper answer of this equation. Integer datatype only decimal value answer return but float and double datatype return answer with floating point then must use float or double datatype in this equation.
I guess you want like this equation, try this
int x = (90/100)*300;

Why is my int 0? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does int exp1 = 14/20*100; equals ‘0’ in java?
I am using this:
int newWidth = Math.round(297 * (40 / 414));
In the code the numbers are actually variables, but those are what they were holding, and when I run the code newWidth comes back as 0, and I was expecting 28 or 29. I don't see what is wrong here...
40 / 414 is rounded to 0 immediately as it only works with integers. You must either cast one of the operands to double/float or use double immediately:
int newWidth = (int)Math.round(297 * (40.0 / 414));
or:
int newWidth = (int)Math.round(297 * ((double)40 / 414));
Because 40/414 equals to 0, so 297*0 = 0 and Math.round(0) = 0
Use doubles instead.
int newWidth = (int)Math.round(297 * (40d / 414));
Change to int newWidth = (int)Math.round(297 * (40.0 / 414));
An int in java uses no decimal places... Therefore you should use a double for this. It is giving you a 0 because inside the () 40/ 414 = 0.
The problem starts with 40/414: that's integer division, which returns an int. In this case: 0.
To fix it, cast one of those ints to a double or float, e.g. ((float) 40 / 414).
The result of 40/414 is zero. This is a consequence of integer division. Change either numerator or denominator to a floating point value to get your desired result
Gonna throw my 2 cents in... as noted, the issue lies in the integer division. Another method for forcing this division to treat the result as a double:
int newWidth = Math.round(297 * (40.0 / 414));
40/414 is rounded to 0.
What you probably want is
int newWidth = (297 * 40) / 414;

Why does int exp1 = 14/20*100; equals '0' in java?

I'm trying to do some basic math and it keeps popping up as 0. I'm sure it has to do with it being an int but I don't know how to work around it. I need to use integers but the math to arrive at those integers uses decimals. How do I do it?
That's integer division.
To get non-integer results, use doubles instead.
This is not special to blackberry, it's standard java behaviour.
This is because you're doing integer math:
int subexpr1 = 14 / 20; // 0
int subexpr2 = subexpr1 * 100; // 0
Use a double instead or change the order
int expr1 = (int) 14.0/20 * 100; // Very small possibility of rounding errors
int expr2 = 14 * 100 / 20; // Will ignore fraction parts
You can change it to 14*100/20 - and then it will give what you want.
I.e. change the sequence of operations (14/20 is 0)
Your result is being cast as an int, so you are losing precision.
Try
double exp1 = 14/20.0*100;

Categories

Resources