From 62c8cd7d4d98f9a2eaaa3dd4c56efc590b33e73f Mon Sep 17 00:00:00 2001 From: John Schember Date: Wed, 27 Jul 2011 19:15:03 -0400 Subject: [PATCH] Store: better comparable price function from Roman Mukhin. --- src/calibre/gui2/store/search/models.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/calibre/gui2/store/search/models.py b/src/calibre/gui2/store/search/models.py index b62b581bd8..e6ef147861 100644 --- a/src/calibre/gui2/store/search/models.py +++ b/src/calibre/gui2/store/search/models.py @@ -22,13 +22,16 @@ 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') - return text + # 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 class Matches(QAbstractItemModel):