python精确定时_Python中的精确处理

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 )

  • trunc() method

    trunc()方法

    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.

    它用于获取数字的截断整数值,它接受数字(整数或浮点数),然后将截断后的实数值返回为整数。

  • ceil() method

    ceil()方法

    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值,它接受数字/数字表达式并返回大于该数字的最小整数值。

  • floor() method

    floor()方法

    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

推荐的帖子

翻译自: https://www.includehelp.com/python/precision-handling.aspx

python精确定时

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值