- 1
Simple decimal code? 2 decimal places %
Example: int num1; int num2 Double totalscore; Sum = num1 + num2 totalscore = (num 1 * 100 / Sum); System.out.println ("Your total score is:" + String.format("%. 2f") , totalscore) + "%") ; //add String.format("%. 2f") , totalscore) if you want 2 decimal places // %. 3f if you want 3 decimal places
1 Answer
+ 1
public class Program {
public static void main(String[] args) {
int num1=33, num2=100, sum;
double totalscore;
sum = num1 + num2;
totalscore = num1 * 100.0 / sum;
String res = String.format(
"Your total score is: %.2f %%", totalscore);
System.out.println (res);
// %.3f if you want 3 decimal places
}
}