当前位置:网站首页>When the decimal places are reserved, there may be a simple solution without rounding
When the decimal places are reserved, there may be a simple solution without rounding
2022-07-18 07:04:00 【Hey, hey, Knight】
stay Python Run the following code :
a = 2.358755
print(a)
print(decimal.Decimal.from_float(a))
print('\n Retain 5 The result of decimal places :')
print(format(a, '.5f'))
Running results :
2.358755
2.358754999999999935056393951526843011379241943359375
Retain 5 The result of decimal places :
2.35875
You can see , The result is not right . actually , Retain 5 The result of decimals is 2.35876
This is due to the principle of the underlying code , Look at number 2 The result of that ,2.358754999999999935056393951526843011379241943359375 , The first 6 The decimal is 4, So we come to 2.35875
Use the following code to solve this problem :
def format_float(data, n):
""" Given data , Retain n Decimal place Example : format(2.358755, '.5f') , The result is 2.35875, The correct rounding value is 2.35876 """
digit = len(str(data).split('.')[-1]) # Calculate the number of decimal places
if n == digit - 1: # The reserved digits are exactly one less than the total decimal digits , Then fill in the following 1
data = float(str(data) + '1')
return format(data, f'.{n}f')
a = 2.358755
print(format_float(a, 5))
Running results :
2.35876
In fact, it complements the original floating-point number 1 And then we can solve this problem , repair 1 Actual data after :
print(decimal.Decimal.from_float(2.3587551))
Running results :
2.358755100000000215487716559437103569507598876953125
You can see the 6 Bit data is now 5, So we can accurately get the rounded value .
边栏推荐
- Idea class document annotation template settings
- 左旋转字符串
- 日志的管理
- PD QC leurre application IC ledry ldr6328s est largement utilisé dans les appareils ménagers de toutes tailles
- MySQL-pymysql模块
- 关于结构体,枚举,联合等自定义类型的笔记
- Debug system
- Swin transformer, the best paper model of iccv 2021, finally started video
- Lenovo Power Management Download
- 复杂链表的复制
猜你喜欢

60 year old developer's suggestion, try to change it!

How to quickly learn SQL for beginners

关于一些字符串相关函数,内存函数及部分模拟

How Oracle escapes single quotation marks

C语言 第九章 字符串

Serialization and deserialization of flip word /maxqueue/ quadratic tree

免驱 USB 转串口 Billboard 芯片(LDR2001)
![[interview must brush 101] hash](/img/01/982c24e923563d74436f91dd4f573e.png)
[interview must brush 101] hash

(一)MATLAB基础知识

CAN协议初介绍
随机推荐
Cron表达式使用
Lifecycle: the foundation of lifecycle aware components - jetpack series (1)
CAN协议初介绍
1-first knowledge of FPGA
PD QC诱骗取电应用IC《乐得瑞LDR6328S》广泛应用于各大小家电
60 year old developer's suggestion, try to change it!
【面试必刷101】双指针
C语言 第六章 函数
Thumbnailator 图片处理类库
关于一些字符串相关函数,内存函数及部分模拟
MySQL-索引
免驱 USB 转串口 Billboard 芯片(LDR2001)
Oscilloscope use concept record
Markdown learning notes Chapter 2 basic grammar (displayed under the markdown editor)
8. MySQL -- 触发器
sensor 原理文章
Markdown learning notes Chapter 2 basic grammar (displayed in non markdown editor)
Lenovo Power Management Download
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (4)
Domestic light! High score spatiotemporal representation learning model uniformer