[CONPY-143] loss of precision with non default locale Created: 2021-02-18  Updated: 2021-03-10  Resolved: 2021-03-10

Status: Closed
Project: MariaDB Connector/Python
Component/s: Generic
Affects Version/s: 1.0.5
Fix Version/s: 1.0.7

Type: Bug Priority: Major
Reporter: Luciano Barcaro Assignee: Georg Richter
Resolution: Fixed Votes: 0
Labels: None


 Description   

Found a strange behavior with float values.

import mariadb
import locale
 
conn = {'host': '192.168.1.2', 'user': 'test', 'password': 'test'}
 
db = mariadb.connect(**conn)
qr = db.cursor()
 
latitude = -24.971077771106
longitude = -53.95170397719726
 
qr.execute('set @lat=?, @lng=?', (latitude, longitude))
 
qr.execute('select @lat, @lng')
lat, lng = qr.fetchone()
print(lat, lng)
# Here, returns what's expected: -24.971077771106 -53.95170397719726
 
locale.setlocale(locale.LC_ALL, 'pt_BR.utf8')
 
qr.execute('select @lat, @lng')
lat2, lng2 = qr.fetchone()
print(lat2, lng2)
# But here, returns: -24.0 -53.0



 Comments   
Comment by Georg Richter [ 2021-02-20 ]

Instead of converting string to double/float using atod (which depends on locale) the string obtained in text protocol should be converted to PyUnicode and then back to PyFloat.

Comment by Georg Richter [ 2021-03-10 ]

Problem was caused by using atod/atof function which depends on locale settings. If locale settings expect a comma instead of decimal point, values were truncated.

Was fixed in f5da77b959f7d8911b29f3b28f0c7f11088d7d43 (unfortunately I combined this fix with bumping version number).

Generated at Thu Feb 08 03:30:34 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.