简体   繁体   English

如果以十进制形式收到的值是空字符串,我如何将其转换为浮点数?

[英]If the value recieved in a Decimal Form is an empty string how can I convert it to a float number?

On my page I ask for various things to the user in the form as it's a calculator that does various things, now you see, the thing is if the user does not have the value for bf I want to give the option to calculate with different values, that is why I required is False, so first what I want to archive is that if the form is empty I want to be passed as a 0 or False, how can I archive this?在我的页面上,我以表格形式向用户询问各种事情,因为它是一个可以做各种事情的计算器,现在你看到了,问题是如果用户没有 bf 的值,我想提供使用不同计算的选项值,这就是为什么我需要的是 False,所以首先我要归档的是,如果表单为空,我想作为 0 或 False 传递,我该如何归档呢?

I have the following form:我有以下表格:

class CMForm(forms.Form):
    age = forms.DecimalField(max_digits=2, decimal_places=0, min_value=15, max_value=80)
    sex = forms.ChoiceField(choices=sex)
    pregnant = forms.BooleanField(initial=False, required=False)
    lactating = forms.BooleanField(initial=False, required=False)
    weight = forms.DecimalField(max_digits=4, decimal_places=1, max_value=635)
    height = forms.DecimalField(max_digits=4, decimal_places=1, max_value=272)
    bf = forms.DecimalField(label='Body Fat(not required)', help_text='%', required=False, decimal_places=1, min_value=1, max_value=80)
    activeness = forms.ChoiceField(choices=activity_level)
    units = forms.ChoiceField(choices=units)

this view:这种观点:

def cmr(request):
    a = float(request.GET['age'])
    bf = float(request.GET.get('bf', 0))
    s = str(request.GET['sex'])
    g = 0
    if s == 'female':
        g = 1
    w = float(request.GET['weight'])
    h = float(request.GET['height'])
    act = str(request.GET['activeness'])
    u = str(request.GET['units'])
    p = str(request.GET.get('pregnant', 0))
    lac = str(request.GET.get('lactating', 0))
    dci = 0
    bmrm = 10*w+6.25*h-5*a+5
    bmrf = 10*w+6.25*h-5*a-161
    bmi = round(w/(h/100)**2, 1)
    if bf is False:
        bf = round(-44.988 + (0.503 * a) + (10.689 * g) + (3.172 * bmi) - (0.026 * bmi**2) + (0.181 * bmi * g) - (0.02 * bmi * a) - (0.005 * bmi**2 * g) + (0.00021 * bmi**2 * a), 1)
    tbf = w*(bf/100)
    ffm = w*(1-(bf/100))
    ffmi = ffm/(h/100)**2
    nffmi = ffmi+6.1*(1.8-(h/100))
    if p == 'on':
        dci = 300
    if lac == 'on':
        dci = dci+500
    if s == 'male':
        bmr = dci+bmrm
        dcis = bmrm*1.2
        dcil = bmrm*1.375
        dcim = bmrm*1.55
        dciv = bmrm*1.725
        dcie = bmrm*1.9
    else:
        bmr = dci+bmrf
        dcis = dci+bmrf*1.2
        dcil = dci+bmrf*1.375
        dcim = dci+bmrf*1.55
        dciv = dci+bmrf*1.725
        dcie = dci+bmrf*1.9

    context = {'dci': round(int(dci)),
               'p': p,
               'lac': lac,
               'bmr': int(bmr),
               'dcis': int(dcis),
               'dcil': int(dcil),
               'dcim': int(dcim),
               'dciv': int(dciv),
               'dcie': int(dcie),
               'act': act,
               'bmi': bmi,
               'bf': bf,
               'tbf': round(tbf, 1),
               'ffm': round(ffm, 1),
               'ffmi': round(ffmi, 1),
               'nffmi': round(nffmi, 1),
               }
    return render(request, "calorie_maintenance_result.html", context)

And I get the following error:我收到以下错误:

could not convert string to float: ''

You can use or ;您可以使用or ; given a string s , s or 0 will evaluate to 0 if s is empty:给定字符串s ,如果s为空,则s or 0将计算为0

s = ''
print(float(s or 0)) # 0.0

s = '123'
print(float(s or 0)) # 123.0

This happens because (in python) x or y returns x if x is true, and y if x is false.发生这种情况是因为(在 python 中) x or y如果x为真则返回x ,如果x为假则返回y Since an empty string is "falsy", '' or 0 is equal to 0 .由于空字符串是“falsy”,因此'' or 0等于0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将字符串转换为浮点数并始终显示 2 个小数位? - How can I convert a string to a float and always show 2 decimal places? 如何将浮点数转换为字符串,但转换为小数点后第六位-Python - How can I convert a float to a string, but to the sixth decimal - Python 无法将字符串数值转换为float - Can't convert string number value to float 不会将字符串格式的十进制数字转换为浮点数 - Wont convert decimal number in string format to float 如何从 web 抓取的数字中删除浮动中的空白空间? 错误:无法将字符串转换为浮点数:'1\xa0364' - How can I remove empty space in float from a number I get from web scraping? Error: could not convert string to float: '1\xa0364' 如何将列表字符串转换为浮点数 - How can I convert the list string to a float 如何检查字符串是否为十进制/浮点数? - How to check if a string is a decimal/ float number? 如何在python中将字符串转换为唯一的十进制数字? - How do I convert a string into a unique decimal number in python? 如何将字符串转换为十进制数以在 Python 中进行算术运算? - How do I convert a string into a decimal number for arithmetic manipulation in Python? 如何将浮点十进制转换为浮点八进制/二进制? - How do I convert float decimal to float octal/binary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM