python精确定时
处理精度 (Handling precisions)
There are many ways to handle/set the precision with a float value, some of the math functions and some of them are based on the formatting.
有很多方法可以使用浮点值来处理/设置精度,其中一些数学函数,其中一些基于格式。
Example without handling precision
没有精确度的示例
# Python program to print float value
import math
a = 10.123456789
b = 10.0
c = 10.134
d = 10 / 3
print("Without precision handling...")
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)
Output
输出量
Without precision handling...
a: 10.123456789
b: 10.0
c: 10.134
d: 3.3333333333333335
1)处理精度的方法 (1) Methods to handle the precision )
-
It is used to get the truncated integer value of a number, it accepts a number (either an integer or a float) and returns the real value truncated to an integral.
它用于获取数字的截断整数值,它接受数字(整数或浮点数),然后将截断后的实数值返回为整数。
-
It is used to get the ceil value of a given number, it accepts a number/numeric expression and returns the smallest integral value which is greater than the number.
它用于获取给定数字的ceil值,它接受数字/数字表达式并返回大于该数字的最小整数值。
-
It is used to get the floor value of a given number, it is used to get the floor value of a number, it accepts a number/numeric expression and returns largest integer (integral) value, which is not greater than the number.
它用于获取给定数字的下限值,用于获取数字的下限值,它接受数字/数字表达式,并返回不大于该数字的最大整数(整数)值。
Python program for precision handing using trunc(), ceil() and floor() methods
使用trunc(),ceil()和floor()方法进行精确处理的Python程序
# Python program for precision handing using
# trunc(), ceil() and floor() methods
import math
a = 10.123456789
b = 10.0
c = 10.134
d = 10 / 3
print("Without precision handling...")
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)
# using trunc() method
print("Using trunc() method...")
print("math.trunc(a): ", math.trunc(a))
print("math.trunc(b): ", math.trunc(b))
print("math.trunc(c): ", math.trunc(c))
print("math.trunc(d): ", math.trunc(d))
# using ceil() method
print("Using ceil() method...")
print("math.ceil(a): ", math.ceil(a))
print("math.ceil(b): ", math.ceil(b))
print("math.ceil(c): ", math.ceil(c))
print("math.ceil(d): ", math.ceil(d))
# using floor() method
print("Using floor() method...")
print("math.floor(a): ", math.floor(a))
print("math.floor(b): ", math.floor(b))
print("math.floor(c): ", math.floor(c))
print("math.floor(d): ", math.floor(d))
Output
输出量
Without precision handling...
a: 10.123456789
b: 10.0
c: 10.134
d: 3.3333333333333335
Using trunc() method...
math.trunc(a): 10
math.trunc(b): 10
math.trunc(c): 10
math.trunc(d): 3
Using ceil() method...
math.ceil(a): 11
math.ceil(b): 10
math.ceil(c): 11
math.ceil(d): 4
Using floor() method...
math.floor(a): 10
math.floor(b): 10
math.floor(c): 10
math.floor(d): 3
2)使用不同的格式化方式处理精度 (2) Handing precision using different ways of formatting )
Using "%"
使用“%”
This method is similar to “printf() function in C language”, it is used to format as well as set the precision.
此方法类似于“ C语言中的printf()函数”,用于设置格式和设置精度。
Using format() method
使用format()方法
This method is used to format a string and we can define the number of precision.
此方法用于格式化字符串,我们可以定义精度数。
Using round() method
使用round()方法
This method rounds the float value with given number of precisions.
此方法以给定的精度舍入浮点值。
Python program for precision handing using %, format() and round() methods
使用%,format()和round()方法进行精度处理的Python程序
# Python program for precision handing
# using % , format() and round() methods
a = 10.123456789
b = 10.0
c = 10.134
d = 10 / 3
print("Without precision handling...")
print("a:", a)
print("b:", b)
print("c:", c)
print("d:", d)
print("Using %...")
print("%.2f" % a)
print("%.2f" % b)
print("%.2f" % c)
print("%.2f" % d)
print("Using format()...")
print("{0:.2f}".format(a))
print("{0:.2f}".format(b))
print("{0:.2f}".format(c))
print("{0:.2f}".format(d))
print("Using round()...")
print(round(a, 2))
print(round(b, 2))
print(round(c, 2))
print(round(d, 2))
Output
输出量
Without precision handling...
a: 10.123456789
b: 10.0
c: 10.134
d: 3.3333333333333335
Using %...
10.12
10.00
10.13
3.33
Using format()...
10.12
10.00
10.13
3.33
Using round()...
10.12
10.0
10.13
3.33
Recommended posts
推荐的帖子
Asking the user for integer input in Python | Limit the user to input only integer value
How to get the hexadecimal value of a float number in python?
Convert an integer value to the string using str() function in Python
Convert a float value to the string using str() function in Python
Taking multiple inputs from the user using split() method in Python
翻译自: https://www.includehelp.com/python/precision-handling.aspx
python精确定时