Rounding up numbers is the most common use case in any programming language that a developer has to deal with.
If you are new to Python Programming and looking for ways to convert a float value with 2 decimal precision, below are various ways to achieve it.
Option 1: using built-in round() function
-
Syntax:
round(number[, ndigits])
Example:
number = 99.981234
rounded_number = round(number, 2)
print(rounded_number)
Output:
99.98
Option 2: using String formatting
-
Example:
number = 23.10124
formatted_number = "{:.2f}".format(number)
print(formatted_number)
Output:
22.10
Option 3: using f-string (Python 3.6+)
-
Example:
number = 13.1234
formatted_number = f"{number:.2f}"
print(formatted_number)
Output:
13.12
Option 4: using decimal module
-
Example:
from decimal import Decimal
number = Decimal('11.1234')
rounded_number = round(number, 2)
print(rounded_number)
Output:
11.12
Option 5: using numpy module
-
Example:
import numpy as np
number = 299.1415
rounded_number = np.round(number, 2)
print(rounded_number)
Output:
299.14
Option 6: using math module
-
Example:
import math
number = 19.18192243
rounded_number = math.floor(number * 100) / 100
print(rounded_number)
Output:
19.18
I have added the links to the official documentation for each example so you can visit and get more details.
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python List of Lists with Examples
- Fix: ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter
- How to Convert Python Notebook .ipynb (JSON) to Executable .py Module
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Pandas Merge Indicator (Left, Right and Both) Example
- Fix: EOFError Exception in Python
- How to URL Decode a Query String in Python
- How to take user input from the console in a Python program
- Compare two lists in Python and return matches
- How to change the Python Default version
- Fix: KeyError: exception in Python
- How to get the Execution Time of A Python Program
- Fix: ModuleNotFoundError: No module named boto3 [Python]
- How to get unique values from a list in Python
- How to pip install Python Modules in VSCode
- Python: Fix - TypeError: NoneType object is not iterable
- How to delete a file using Python code example
- How to create a dictionary comprehension in Python
- Python: Get just the filename without extension using Path
- How to comment out a block of code in Python
- How to add two float numbers in Python
- Python: How to POST Json Data with HTTP Request
- Get MD5 Hash as Checksum of a String in Python
- Python - Convert float to String
- How to Parse XML String in Python
More Posts:
- [fix] Java Spring Boot JPA SQLSyntaxErrorException: Encountered user at line 1 column 14 - Java
- Todays Apple Spring Loaded Event Live Updates - Apple
- [fix] MySQL cj jdbc CommunicationsException: Communications link failure - Java
- Ways to Initialize HashMap Collection in Java - Java
- How to Remove All Terminated Console tabs at once in Eclipse - Eclipse
- List all Username and User ID using Bash Command - Bash
- [Updated] macOS 13 Ventura Supported Mac Devices List - MacOS
- What is HTTP 500 Internal Server Error Code on web browsers - HowTos