Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Currency Style by pasharvs
import re
def checkio(text):
def toUSstyle(matchobj):
number = matchobj.group(0)
number = number.replace('.',',')
number = re.sub(r'([\d\,]+)\,(\d\d)$', r'\1.\2', number)
return number
return re.sub(r'\$((?:\d+\.)*\d+(?:\,\d+)?)(?![,\d])', toUSstyle, text)
#I SPENT F**CKING 4 HOURS ON THIS!!!!!!!!
#IT IS 3 a.m AND IT SEEMS LIKE I'M NOT GOING IN UNIVERSITY TOMORROW
#BUT NOW I KNOW A LITTLE MORE HOW REGULAR EXPRESSION WORKS
#NEVERTHELESS, I WILL BE STUCKING AGAIN AND AGAIN ON THIS DAMN REGEX
# :-_(
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio("$1.234.567,89") == "$1,234,567.89" , "1st Example"
assert checkio("$0,89") == "$0.89" , "2nd Example"
assert checkio("Euro Style = $12.345,67, US Style = $12,345.67") == \
"Euro Style = $12,345.67, US Style = $12,345.67" , "European and US"
assert checkio("Us Style = $12,345.67, Euro Style = $12.345,67") == \
"Us Style = $12,345.67, Euro Style = $12,345.67" , "US and European"
assert checkio("$1.234, $5.678 and $9") == \
"$1,234, $5,678 and $9", "Dollars without cents"
Sept. 21, 2015
Comments: