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
def comparable_price(text):
text = re.sub(r'[^0-9.,]', '', text)
delimeter = (',', '.')
if len(text) < 3 or text[-3] not in delimeter:
text += '00'
text = re.sub(r'\D', '', text)
text = text.rjust(6, '0')
# this keep thousand and fraction separators
match = re.search(r'(?:\d|[,.](?=\d))(?:\d*(?:[,.\' ](?=\d))?)+', text)
if match:
# replace all separators with '.'
m = re.sub(r'[.,\' ]', '.', match.group())
# 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