LearnFrenzy
Sign in Take a free test

Python Built-in Functions · Find the Error · Question 4

Bug: 2.675 rounds to 2.67 Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

A bill of ₹2.675 should be rounded half up to ₹2.68. Which statement correctly identifies the bug?

main.py
amount = 2.675
print(round(amount, 2))
  • A The float 2.675 is really 2.67499…, and round() also rounds halves to even; use decimal.Decimal("2.675").quantize(Decimal("0.01"), ROUND_HALF_UP) Correct
  • B round() only rounds to whole numbers
  • C The second argument is the number of significant figures
  • D print truncates floats
Correct Answer: Option A

For money, use Decimal built from strings with an explicit rounding mode.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links