diff --git a/xblocks_contrib/problem/capa_block.py b/xblocks_contrib/problem/capa_block.py index bee1c199..e273eccf 100644 --- a/xblocks_contrib/problem/capa_block.py +++ b/xblocks_contrib/problem/capa_block.py @@ -1025,6 +1025,10 @@ def get_display_progress(self): progress = self.get_progress() score, total = progress.frac() if progress else (0, 0) + # When the weight does not divide evenly between the questions, the score is a repeating + # fraction (e.g. 0.3333333333333333). This rounds it the same way `Progress.__str__` does. + score = round(score, 2) + # Withhold the score if hiding correctness if not self.correctness_available(): score = None diff --git a/xblocks_contrib/problem/tests/test_capa_block.py b/xblocks_contrib/problem/tests/test_capa_block.py index 0220683d..43610a86 100644 --- a/xblocks_contrib/problem/tests/test_capa_block.py +++ b/xblocks_contrib/problem/tests/test_capa_block.py @@ -2833,6 +2833,24 @@ def test_get_display_progress_show_correctness(self, show_correctness, is_correc assert score == expected_score assert total == 1 + @ddt.data( + (1, 3, 0.33), + (2, 3, 0.67), + (1, 2, 0.5), + (3, 3, 1), + ) + @ddt.unpack + def test_get_display_progress_rounds_the_score(self, raw_earned, raw_possible, expected_score): + """ + Check that the displayed score is rounded to at most two decimal places. + """ + block = CapaFactory.create(attempts=1) + block.weight = 1 + block.score = Score(raw_earned=raw_earned, raw_possible=raw_possible) + score, total = block.get_display_progress() + assert score == expected_score + assert total == 1 + def test_get_html(self): """ Check that get_html() calls get_progress() with no arguments.