BOOKPYTHON Telegram 3738
Функция round округляет число до заданной точности в десятичных знаках.


>>> round(1.2)
1
>>> round(1.8)
2
>>> round(1.228, 1)
1.2


Также можно задать отрицательную точность:


>>> round(413.77, -1)
410.0
>>> round(413.77, -2)
400.0


round возвращает значение того же типа, что и входное число:


>>> type(round(2, 1))
<class 'int'>

>>> type(round(2.0, 1))
<class 'float'>

>>> type(round(Decimal(2), 1))
<class 'decimal.Decimal'>

>>> type(round(Fraction(2), 1))
<class 'fractions.Fraction'>


Для собственных классов можно определить поведение округления с помощью метода __round__:


>>> class Number(int):
... def __round__(self, p=-1000):
... return p
...
>>> round(Number(2))
-1000
>>> round(Number(2), -2)
-2


Значения округляются до ближайшего кратного 10 ** (-precision). Например, при precision=1, значение округляется до кратного 0.1: round(0.63, 1) возвращает 0.6.
Если два значения одинаково близки, округление происходит в сторону чётного числа:


>>> round(0.5)
0
>>> round(1.5)
2


Иногда округление чисел с плавающей точкой может казаться неожиданным:


>>> round(2.85, 1)
2.9


Это связано с тем, что большинство десятичных дробей не могут быть точно представлены в формате float:


>>> format(2.85, '.64f')
'2.8500000000000000888178419700125232338905334472656250000000000000'


Если нужно округление "в большую сторону при 0.5" (round half up), можно использовать decimal.Decimal:


>>> from decimal import Decimal, ROUND_HALF_UP
>>> Decimal(1.5).quantize(0, ROUND_HALF_UP)
Decimal('2')
>>> Decimal(2.85).quantize(Decimal('1.0'), ROUND_HALF_UP)
Decimal('2.9')
>>> Decimal(2.84).quantize(Decimal('1.0'), ROUND_HALF_UP)
Decimal('2.8')


👉@BookPython
👍4



tgoop.com/BookPython/3738
Create:
Last Update:

Функция round округляет число до заданной точности в десятичных знаках.


>>> round(1.2)
1
>>> round(1.8)
2
>>> round(1.228, 1)
1.2


Также можно задать отрицательную точность:


>>> round(413.77, -1)
410.0
>>> round(413.77, -2)
400.0


round возвращает значение того же типа, что и входное число:


>>> type(round(2, 1))
<class 'int'>

>>> type(round(2.0, 1))
<class 'float'>

>>> type(round(Decimal(2), 1))
<class 'decimal.Decimal'>

>>> type(round(Fraction(2), 1))
<class 'fractions.Fraction'>


Для собственных классов можно определить поведение округления с помощью метода __round__:


>>> class Number(int):
... def __round__(self, p=-1000):
... return p
...
>>> round(Number(2))
-1000
>>> round(Number(2), -2)
-2


Значения округляются до ближайшего кратного 10 ** (-precision). Например, при precision=1, значение округляется до кратного 0.1: round(0.63, 1) возвращает 0.6.
Если два значения одинаково близки, округление происходит в сторону чётного числа:


>>> round(0.5)
0
>>> round(1.5)
2


Иногда округление чисел с плавающей точкой может казаться неожиданным:


>>> round(2.85, 1)
2.9


Это связано с тем, что большинство десятичных дробей не могут быть точно представлены в формате float:


>>> format(2.85, '.64f')
'2.8500000000000000888178419700125232338905334472656250000000000000'


Если нужно округление "в большую сторону при 0.5" (round half up), можно использовать decimal.Decimal:


>>> from decimal import Decimal, ROUND_HALF_UP
>>> Decimal(1.5).quantize(0, ROUND_HALF_UP)
Decimal('2')
>>> Decimal(2.85).quantize(Decimal('1.0'), ROUND_HALF_UP)
Decimal('2.9')
>>> Decimal(2.84).quantize(Decimal('1.0'), ROUND_HALF_UP)
Decimal('2.8')


👉@BookPython

BY Библиотека Python разработчика | Книги по питону


Share with your friend now:
tgoop.com/BookPython/3738

View MORE
Open in Telegram


Telegram News

Date: |

How to Create a Private or Public Channel on Telegram? On Tuesday, some local media outlets included Sing Tao Daily cited sources as saying the Hong Kong government was considering restricting access to Telegram. Privacy Commissioner for Personal Data Ada Chung told to the Legislative Council on Monday that government officials, police and lawmakers remain the targets of “doxxing” despite a privacy law amendment last year that criminalised the malicious disclosure of personal information. The best encrypted messaging apps 6How to manage your Telegram channel? Commenting about the court's concerns about the spread of false information related to the elections, Minister Fachin noted Brazil is "facing circumstances that could put Brazil's democracy at risk." During the meeting, the information technology secretary at the TSE, Julio Valente, put forward a list of requests the court believes will disinformation.
from us


Telegram Библиотека Python разработчика | Книги по питону
FROM American