# -*- coding: cp949 -*-
# commanumber.py
import string
def comma_number(number):
if number[0] in ['+', '-']:
sign_mark, number = number[:1], number[1:]
else:
sign_mark = ''
try:
tmp = string.split(number, '.')
num = tmp[0]; decimal = '.' + tmp[1]
except:
num = number; decimal = ''
head_num = len(num) % 3
result = ''
# ~~~~~
for pos in range(len(num)):
if pos == head_num and head_num:
result = result + ','
elif (pos - head_num) % 3 == 0 and pos:
result = result + ','
result = result + num[pos]
# ~~~~~
return sign_mark + result + decimal
print comma_number(" 12345678.345678")^m<="" p="">
# AL GO RI SM 알고리즘
# 1. 부호가 있나 검사
# 2. 소숫점(.) 이 있으면 기준잡고 앞부분만 head_num 에 담음
"""
질문:
~~~~~ 부터 ~~~~~ 까지 소스코드에서,
소스코드 해석.
코드가 이해가 안됨.
"""
# -*- coding: cp949 -*- # commanumber.py import string def comma_number(number): if number[0] in ['+', '-']: sign_mark, number = number[:1], number[1:] else: sign_mark = '' try: tmp = string.split(number, '.') num = tmp[0]; decimal = '.' + tmp[1] except: num = number; decimal = '' head_num = len(num) % 3 result = '' # ~~~~~ for pos in range(len(num)): if pos == head_num and head_num: result = result + ',' elif (pos - head_num) % 3 == 0 and pos: result = result + ',' result = result + num[pos] # ~~~~~ return sign_mark + result + decimal print comma_number(" 12345678.345678")
'스터디 > Jump2Python' 카테고리의 다른 글
tab 2 4 space (0) | 2015.01.12 |
---|---|
Simple Memo (0) | 2015.01.12 |