保留四位有效数字python_Python计算有效数字
我将这个问题标记为javascript,因为即使我目前是用Python编写的,如果用javascript实现更容易,我也可以很容易地用javascript实现它。在
我的任务是为化学系做一个重要的数字计算检查。这意味着学生将他们的数据输入到字段中,web应用程序将对他们的字段执行预定义的操作,并跟踪有效数字,并查看他们的答案是否具有适当数量的有效数字。在
当我把问题分解成我认为是一个好的工作流程时,我意识到我需要一种方法来确定有效数字的数量,无论是Python(后端,因为这是一个用Django制作的web应用程序)或Javascript(因为您可以始终在前端验证它,没有问题)来确定有效位数。我做了一点研究,遇到了this question,它告诉我需要使用python字符串而不是float。我当前的python代码感觉几乎完成了,但是我仍然面临一个主要的挑战import re
def find_sigfigs(x):
# change the 'E' to lower case if the student typed it in as uppercase
x = x.lower()
if ('e' in x):
myStr = x.split('e')
# this function assumes that the number on the left of the 'e' is
# the number of sigfigs. That would be true for user input but not
# true when python converts a float to scientific notation
return len( (re.search('[0-9]+', myStr[0])).group() )
else:
# put it in e format and return the result of that
### problem: python makes me hard code the number of sigfigs as '2'
### without the 2 there it always defaults to 6
return find_sigfigs('%.*e' %(2,float(x)))
>>> find_sigfigs('1.3e-4')
>>> 2
>>> find_sigfigs('1234')
>>> 3
>>> find_sigfigs('123456')
>>> 3
>>> find_sigfigs('1.2345e3')
>>> 5
如果没有2
^{pr2}$
简单地说,我的问题是我需要一种简单的方法来计算sigfig,当它不是由学生明确声明的时候(也就是说,当它是科学记数法的时候)。有没有什么简单的方法可以让我把“e”前的每一个0减到第一个非零位数。我想,我需要从拆分字符串的后面开始,去掉零,直到它变成一个非零数字?在
编辑:因此,我希望这是一个适当的解决问题的方法。我测试了几次,但不是太严格(也就是说它可能有用,但谁知道呢!我不太擅长打手势……)def find_sigfigs(x):
'''Returns the number of significant digits in a number. This takes into account
strings formatted in 1.23e+3 format and even strings such as 123.450'''
# change all the 'E' to 'e'
x = x.lower()
if ('e' in x):
# return the length of the numbers before the 'e'
myStr = x.split('e')
return len( myStr[0] ) - 1 # to compenstate for the decimal point
else:
# put it in e format and return the result of that
### NOTE: because of the 8 below, it may do crazy things when it parses 9 sigfigs
n = ('%.*e' %(8, float(x))).split('e')
# remove and count the number of removed user added zeroes. (these are sig figs)
if '.' in x:
s = x.replace('.', '')
#number of zeroes to add back in
l = len(s) - len(s.rstrip('0'))
#strip off the python added zeroes and add back in the ones the user added
n[0] = n[0].rstrip('0') + ''.join(['0' for num in xrange(l)])
else:
#the user had no trailing zeroes so just strip them all
n[0] = n[0].rstrip('0')
#pass it back to the beginning to be parsed
return find_sigfigs('e'.join(n))
保留四位有效数字python_Python计算有效数字相关推荐
- python保留四位小数_保留四位有效数字python
信息举报 时间:2020-11-29 本页为您甄选多篇描写保留四位有效数字python,保留四位有效数字python精选,保留四位有效数字python大全,有议论,叙事 ,想象等形式.文章字数有400 ...
- python的print保留四位小数——TPR计算器——二分类TPR——五分类TPR
仅作为记录,大佬请跳过. 文章目录 print保留四位小数 二分类TPR计算器 五分类TPR计算器 全部代码 使用 展示 参考 print保留四位小数 print(format(TPR,'.4f')) ...
- NPOI导出数值格式设置(我是保留四位小数,不足补0)
看了网上好多帖子,都是保留两位小数的,写法是: HSSFDataFormat.GetBuiltinFormat("0.00"); 于是想四位小数,就是多加两个00,变成: HSSF ...
- 设z服从标准正态分布_【填空题】设Z服从标准正态分布,则 (保留四位小数)
[填空题]设Z服从标准正态分布,则 (保留四位小数) 更多相关问题 听力原文:I have no money with me now.How about lending me 100 yuan?(4) ...
- 有效数字的保留规则python_关于小数有效数字保留的方法
1. DecimalFormat df = new DecimalFormat("######0.00"); double d1 = 3.23456 double d2 ...
- java double保留四位小数_double类型数据保留四位小数的另一种思路
来源:透析公式处理,有时候数据有很长的小数位,有的时候由在四位以内,如果用一般的处理方法,那么不足四位的小树会补充0到第四位,这样子有点画蛇添足的感觉,不太好看.所以要根据小数的位数来处理判断. 思路 ...
- matlab不显示到小数点后四位,matlab保留四位小数
代表之前打开的文件 '%8.4f%8.3f%6.2f\\n'输出格式, 具体如下: 1. %8.4f 表示输出格式为浮点数, 占用 8 位( 含小数点) , 保留 4 资料内容仅供您学...... % ...
- 科学计算机法后保留四位小数,四舍五入(一种精确度的计数保留法)_百度百科...
[sì shě wǔ rù] 四舍五入 (一种精确度的计数保留法) 语音 编辑 锁定 讨论 上传视频 四舍五入是一种精确度的计数保留法,与其他方法本质相同.但特殊之处在于,采用四舍五入,能使被保留部分 ...
- 基于链表的四位存储大数计算
数据结构的大型实验,和之前写的计算器不同的是,少了词法解析,多了链表指针操作,写完感觉,指针操作能力上了一个台阶,有时间有兴趣的亲,也可以尝试一下,下附实验报告及源码. 1.实验内容 1.1实验目的 ...
最新文章
- tf.nn.l2_loss() 与 权重衰减(weight decay)
- 教你用深度学习LSTM网络预测流行音乐趋势(附代码)
- 如何在家写出一份无懈可击的产品文档
- ZedGraph怎样实现将图形右键菜单的打印和页面设置合并为打印的二级子菜单
- matlab中的@函数
- AUTOSAR从入门到精通100讲(九十八)-CANoe之Panel Designer的使用
- GWT(Google Web Tookit) Eclipse Plugin的zip下载地址(同时提供GWT Designer下载地址)
- 通用apdu指令_8086微处理器中的通用指令格式
- DPDK 16.04/16.11.2 默认tx offload是关闭的引起tx vlan offload无效
- 得到进程id_GDB调试多进程程序
- CCF NOI1049 旋转图像
- easyUI 属性总结
- Python学习笔记(五)——读写文件
- 用photoshop去掉图片文字
- 5.交换机环路与VLAN
- 计算机网络——网络安全(湖科大 教书匠)
- 以中断方法设计单片机秒、分脉冲发生器
- 五大最受欢迎的BUG管理系统 .
- mysql记录到毫秒,记录到微妙
- 【Springboot+PageHelper】Springboot 添加PageHelper 分页