3

How to convert an input number 34,6 to decimal form 34.6 in python? I made simple program in pyqt4 GUI and in Germany the Germans consider "," as "." so how can I convert my input "a" (line Edit) number to decimal ?

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))
  • I think what you are looking for is the locale module (here is an answer that should help http://stackoverflow.com/a/1858700/16959) – Jason Sperske May 28 '15 at 13:26

1 Answers1

4

As per in : Here

Try this

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

Maybe your code could be:

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))
Community
  • 1
  • 1
Solano
  • 550
  • 2
  • 9
  • If your question is about the return, it will be float. return of math.pow is a float number – Solano May 28 '15 at 13:50