round()
The built-in function that rounds a number to a given number of decimal places, returning a new number.
In more detail
round(2.675, 2) gives 2.67, not 2.68, because the float is really slightly below 2.675, and exact halves round to the even neighbour. Round the value only when you need the rounded number; to change only what is shown, use a format specifier.
Example
round(3.14159, 2) # 3.14Related terms
- format specifier — The instructions after a colon inside a replacement field, such as .2f for two decimal places, that control how a value is shown.
- floating-point rounding error — The tiny inaccuracy in float results caused by storing decimal fractions in binary, such as 0.1 + 0.2 giving 0.30000000000000004.
- format() — The built-in function that turns a value into a string using a format specifier, as in format(3.14159, ".2f").
Taught in
- Types And Functions, A Whirlwind Tour — Hands-on Python: Complete Video Course & Book
- Division, Quotients And Remainders — Hands-on Python: Complete Video Course & Book
- Variables And Assignment — Hands-on Python: Complete Video Course & Book
- Exponents, Roots And Float Formats — Hands-on Python: Complete Video Course & Book
- Compound Boolean Expressions — Hands-on Python: Complete Video Course & Book
- Thirty Questions, One Sneaky Print — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Operators And Who Binds First — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Tuples, And The Comma That Makes Them — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Mock, Review And Exam Day — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Import Variants, dir() And sys.path — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- math, random And platform — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Modules Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Methods And The self Parameter — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Inheritance, super() And Polymorphism — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Special Methods: __str__, __eq__ And Friends — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Generators And Iterators — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Mock, Review And Exam Day — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep