我将这个问题标记为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