How does python output a value according to the number of digits after the decimal point?
This article mainly introduces the relevant knowledge of "python how to output a certain value according to the number of digits of value precision after the decimal point." Xiaobian shows you the operation process through actual cases. The operation method is simple and fast and practical. I hope this article "python how to output a certain value according to the number of digits of value precision after the decimal point" can help you solve the problem.
import mathdef round(number, ndigits=0): e = 10**ndigits return math.floor(number*e+0.5)/efor i in range(10): print(i, round(3.141592653, i))
from _pydecimal import Decimal, Context, ROUND_HALF_UPprint(Context(prec=3, rounding=ROUND_HALF_UP).create_decimal('1.325'))
def round_up(value):# Replace the built-in round function to retain the precision of two decimal places return round(value *100) / 100.0print(round(1.4))print(round(1.5))print(round(1.5, 1))print(round(1.5, 2))print(round_up(1.115))print(round_up(1.125))print(Decimal(3.14159))
The content of "how python realizes outputting a certain value according to the number of digits of value precision after decimal point" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.