博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python精确定时_Python中的精确处理
阅读量:2531 次
发布时间:2019-05-11

本文共 4019 字,大约阅读时间需要 13 分钟。

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 valueimport matha = 10.123456789b = 10.0c = 10.134d = 10 / 3print("Without precision handling...")print("a:", a)print("b:", b)print("c:", c)print("d:", d)

Output

输出量

Without precision handling...a: 10.123456789b: 10.0c: 10.134d: 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() methodsimport matha = 10.123456789b = 10.0c = 10.134d = 10 / 3print("Without precision handling...")print("a:", a)print("b:", b)print("c:", c)print("d:", d)# using trunc() methodprint("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() methodprint("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() methodprint("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.123456789b: 10.0  c: 10.134d: 3.3333333333333335Using trunc() method... math.trunc(a):  10math.trunc(b):  10math.trunc(c):  10math.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):  10math.floor(b):  10math.floor(c):  10math.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() methodsa = 10.123456789b = 10.0c = 10.134d = 10 / 3print("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.123456789b: 10.0c: 10.134d: 3.3333333333333335Using %...10.1210.0010.133.33Using format()...10.1210.0010.133.33Using round()...10.1210.0 10.133.33

Recommended posts

推荐的帖子

翻译自:

python精确定时

转载地址:http://ehazd.baihongyu.com/

你可能感兴趣的文章
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
【Linux网络编程】使用GDB调试程序
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>