如何将输入数字 34,6 转换为 Python 中的十进制形式 34.6

How to convert an input number 34,6 to decimal form 34.6 in Python

如何在python中将输入的数字34,6转换为十进制形式的34.6?我在 pyqt4 GUI 中制作了简单的程序,在德国,德国人将“,”视为“。”那么如何将输入的 "a"(行编辑)数字转换为十进制数?

def test(self): 
    a = int(self.ui.lineEdit.text()) 
    b = int (self.ui.lineEdit_2.text()) 
    Result = math.pow((a+b),2) 
    self.ui.lineEdit_3.setText(str(Result))

根据:Here

试试这个

from locale import *
setlocale(LC_NUMERIC, '') # set to your default locale; for me this is
# 'English_Canada.1252'. Or you could explicitly specify a locale in which floats
# are formatted the way that you describe, if that's not how your locale works :)
atof('123,456') # 123456.0
# To demonstrate, let's explicitly try a locale in which the comma is a
# decimal point:
setlocale(LC_NUMERIC, 'French_Canada.1252')
atof('123,456') # 123.456

也许您的代码可以是:

from locale import *
def test(self):
    setlocale(LC_NUMERIC, 'French_Canada.1252')
    a = atof(self.ui.lineEdit.text()) 
    b = atof(self.ui.lineEdit_2.text()) 
    Result = math.pow((a+b),2) 
    self.ui.lineEdit_3.setText(str(Result))