Python function round()
Allows to round up numbers with or without decimal. The function has two possible parameters and is directly integrated into the programming language.
What is Python round()
And what is this function for?
If you want to round numbers in the Python programming language, the Python integrated function round()
is the method The simplest and most efficient. It returns a floating comma number which is rounded from a specified number. By default, the number of figures after the point is « 0 », which means that the whole number closest to your entry is returned. You can also Define the number of figures after the comma. We will present these two methods in the following sections.
Managed NextCloud of Ionos Cloud
Work as a team in your own cloud
- Data security
- Integrated collaboration tools
- Accommodation in European data centers
Python syntax and parameters round()
First a look at the python syntax round()
and its parameters. Here is the generic structure of the function:
round(nombre, décimales)
python
The « number » parameter is compulsory. You put the number you want to round up or down. On the other hand, « decimals » is a parameter optional. You can indicate here the number of decimals to which your number must be rounded. The default is « 0 ». If you do not use this parameter, the numbers after the point will be rounded to the nearest whole. If you indicate the value « 2 », you will get a two -digit rounded number after the comma.
Python's example round()
decimal
In the following example, we show you how to use the Python function round()
Without the « decimal » parameter. To do this, we start by defining the « number » parameter, then we ask the system to perform the function. As the function is already included in the installation of Python, it is not necessary to make other measures. Here is the corresponding code:
nombre = 3.7835738292
nombre_arrondi = round(nombre)
print(nombre_arrondi)
python
If you run this code, you will get the following result:
You can also include the value of the « number » parameter directly in the Python function round()
. The code of our example above will then look like this:
nombre_arrondi = round(3.7835738292)
print(nombre_arrondi)
python
The release will be « 4 » again.
Python's example round()
with decimals
If you use the « decimal » setting, you can use Python round()
even more precisely according to your needs. To do this, we extend our example above and ask the system to round our number and display the result with two decimals. The appropriate code for this operation is as follows:
nombre = 3.7835738292
nombre_arrondi = round(nombre, 2)
print(nombre_arrondi)
python
We can also write the code with the value of « number » directly in python round()
::
nombre_arrondi = round(3.7835738292, 2)
print(nombre_arrondi)
python
This does not change the result. In both cases, the exit will be as follows:
Advice
Now deploy websites and applications directly with GitHub: with Deploy Now from Ionos, you use automated deployments and benefit from a quick installation and sustainable scalability. Find the type of project that meets your needs!