Store: better comparable price function from Roman Mukhin.

This commit is contained in:
John Schember 2011-07-27 19:15:03 -04:00
parent a2a2c10ca1
commit 62c8cd7d4d

View File

@ -22,12 +22,15 @@ from calibre.utils.icu import sort_key
from calibre.utils.search_query_parser import SearchQueryParser from calibre.utils.search_query_parser import SearchQueryParser
def comparable_price(text): def comparable_price(text):
text = re.sub(r'[^0-9.,]', '', text) # this keep thousand and fraction separators
delimeter = (',', '.') match = re.search(r'(?:\d|[,.](?=\d))(?:\d*(?:[,.\' ](?=\d))?)+', text)
if len(text) < 3 or text[-3] not in delimeter: if match:
text += '00' # replace all separators with '.'
text = re.sub(r'\D', '', text) m = re.sub(r'[.,\' ]', '.', match.group())
text = text.rjust(6, '0') # remove all separators accept fraction,
# leave only 2 digits in fraction
m = re.sub(r'\.(?!\d*$)', r'', m)
text = '{0:0>8.0f}'.format(float(m) * 100.)
return text return text