522.Calculate a Percentage
Easy
Given the marks
obtained and the maximum total, return the percentage scored as text with two decimal places.
A percentage is a fraction rescaled so the whole is 100. The arithmetic is simple, but the order of operations and the types matter. If both values stay ints, obtained / total is 0 for anything below full marks. The two solutions show two safe orders: divide-then-scale with an explicit cast, and scale-then-divide with a double literalDouble LiteralA number written with a decimal point, such as 100.0, which the compiler treats as a double instead of an int. that makes the whole expression decimal.
Example 1:
Input: obtained = 437, total = 500
Output: "87.40"
Example 2:
Input: obtained = 1, total = 3
Output: "33.33"
+ 6 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ total ≤ 1000000 - ●
0 ≤ obtained ≤ total
obtained =
437
total =
500