Is there any way to calculate (for example) 50% of 120? I tried:
int k = (int)(120 / 100)*50;
But it doesn't work.
Percentage = (Obtained score x 100) / Total Score To get these parameters (inputs) from the user, try using the Scanner function in Java.
The following code sample shows how to format a percentage. Double percent = new Double(0.75); NumberFormat percentFormatter; String percentOut; percentFormatter = NumberFormat.
In Java, the modulus operator is a percent sign, %. The syntax is the same as for other operators: int quotient = 7 / 3; int remainder = 7 % 3; The first operator, integer division, yields 2.
10 percent means one tenth. To calculate 10 percent of a number, simply divide it by 10 or move the decimal point one place to the left. For example, 10 percent of 230 is 230 divided by 10, or 23.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With