正剧开始:
星历2016年02月02日 10:00:05, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究小数。
>>> 零点五八 三点五 四十一点四七 if __name__ == '__main__': a = [0.58,3.5,41.47]; for i in range(len(a)): print(floatNumberRead(a[i])); ### # @usage 小数的读法 # @author mw # @date 2016年01月21日 星期四 09:53:40 # @param # @return # ### def floatNumberRead(num): #正负号 if (num < 0): sign = '负'; else: sign = ''; #判断是否小数 sOfNum = str(num); index = sOfNum.find('.'); if (index != -1): chineseOfNumber=['零','一', '二', '三', '四', '五', '六',\ '七', '八', '九']; sOfNum = sOfNum[index+1:]; decimalPart = '点'; for i in range(len(sOfNum)): decimalPart += chineseOfNumber[int(sOfNum[i])]; import math; intPart = numberToChinese(math.floor(abs(num)), ''); return sign+intPart+decimalPart; else: return sign+numberToChinese(num, ''); >>> 一点四 写作 1.4 五点八 写作 5.8 零点零九 写作 0.09 零点八八 写作 0.88 ### # @usage 小数的写法 # @author mw # @date 2016年02月02日 星期二 09:19:33 # @param # @return # ### def floatNumberWrite(s): #小数点位置 index = s.find('点'); #整数部分字符串 sIntPart = s[:index]; #小数部分字符串 sFloatPart = s[index+1:]; #小数精度位数 accuracy = len(sFloatPart); #print(sIntPart); #print(sFloatPart); chineseOfNumber=['零','一', '二', '三', '四', '五', '六',\ '七', '八', '九']; floatPart = 0; floatRate = 0.1; for i in range(accuracy): for j in range(10): if chineseOfNumber[j] == sFloatPart[i]: floatPart += j * floatRate; floatRate*=0.1; break; intPart = chineseToNumber(sIntPart); result = round(intPart + floatPart, accuracy); return result; ### # @usage 数字的中文写法 # @author mw # @date 2016年01月08日 星期五 12:37:26 # @param # @return # ### def numberToChinese(num, s): if (num < 0): num = abs(num); chineseOfNumber=['零','一', '二', '三', '四', '五', '六',\ '七', '八', '九', '十','百','千','万','亿']; bit = 0; tmp = num; if (tmp == 0): s = chineseOfNumber[0]; while (tmp > 0): tmp = tmp//10; bit+=1; tmp = num; while (tmp > 0): if (tmp < 10): s += chineseOfNumber[tmp]; tmp -= 10; elif (tmp < 100): s += chineseOfNumber[tmp//10]; s += '十'; tmp = tmp%10; elif (tmp < 1000): s += chineseOfNumber[tmp//100]; s += '百'; tmp = tmp%100; if tmp < 10 and tmp > 0: s += '零'; elif (tmp < 10000): s += chineseOfNumber[tmp//1000]; s += '千'; tmp = tmp%1000; if tmp < 100 and tmp > 0: s += '零'; elif (tmp < 100000000): s1 = ''; s += numberToChinese(tmp//10000, s1); s += '万'; tmp =tmp%10000; if tmp < 1000 and tmp > 0: s += '零'; elif (tmp >= 100000000): s1 = ''; s += numberToChinese(tmp//100000000, s1); s += '亿'; tmp = tmp%100000000; if tmp < 10000000 and tmp > 0: s += '零'; else: pass; return s; ### # @usage 数字的中文写法转化为数字 # @author mw # @date 2016年01月08日 星期五 09:19:55 # @param # @return # ### def chineseToNumber(s): chineseOfNumber=['零','一', '二', '三', '四', '五', '六',\ '七', '八', '九', '十','百','千','万','亿']; result = 0; #每一组两个数,比如九百,一万,都是由一个值数和一个倍数组成。 #不可能两个以上的值在一块,但可能两个以上的倍数在一块,比如九九不合法,但四百万合法。 #合法表达为0,不合法为其它值 illegal = 0; #两个长度 lengthOfStr = len(s); lengthOfChs = len(chineseOfNumber); #合法性判断 for i in range(lengthOfStr): if illegal == 1: break; for j in range(lengthOfChs): if s[i] == chineseOfNumber[j]: break; else: if j >= lengthOfChs-1: print('含有非中文数字的字符,表达式不合法'); illegal = 1; for i in range(lengthOfStr-1): if illegal == 1: break; for j in range(10): if s[i] == chineseOfNumber[j]: if j>0: for k in range(10): if s[i+1] == chineseOfNumber[k]: print('连续两个本数相连而没有倍数,表达式不合法。'); illegal = 1; break; #当这个数是零时,它后面跟零或倍数都不合法 else: if s[i+1] == chineseOfNumber[0]: print('连续两个零相连,表达式不合法。'); illegal = 1; break; for k in range(10, lengthOfChs): if s[i+1] == chineseOfNumber[k]: print('零后面跟上倍数,表达式不合法。'); illegal = 1; break; for i in range(lengthOfStr-1): if illegal == 1: if (i > 0): print('表达式的倍数排序不符合规范,不合法。'); break; if s[i] == '十': if s[i+1] == '十' or s[i+1] == '百' or s[i+1] == '千': illegal = 1; elif s[i] == '百': if s[i+1] == '十' or s[i+1] == '百' or s[i+1] == '千': illegal = 1; elif s[i] == '千': if s[i+1] == '十' or s[i+1] == '百' or s[i+1] == '千': illegal = 1; elif s[i] == '万': if s[i+1] == '十' or s[i+1] == '百' or s[i+1] == '千': illegal = 1; elif s[i] == '亿': if s[i+1] == '十' or s[i+1] == '百' or s[i+1] == '千' or s[i+1] == '万': illegal = 1; else: pass; #合法则计算 if illegal!=0: print('输入不合法。'); else: value = 0; multiple = 1; result = 0; #超过亿的部分,单独分出来的原因是避免再和万的倍数相乘 yiPart = 0; #超过万的部分 wanPart = 0; for i in range(lengthOfStr): if s[i] == '亿': result += value+wanPart+yiPart; multiple = 100000000; value = result; result = value*multiple; if (i < lengthOfStr-1 and s[i+1] == '亿'): value = 0; else: yiPart = result; result = 0; multiple = 1; elif s[i] == '万': result += value+wanPart; multiple = 10000; value = result; result = value*multiple; if (i < lengthOfStr-1 and (s[i+1] == '亿' or s[i+1] == '万')): value = 0; else: if (result > 100000000): yiPart = result; else: wanPart = result; result = 0; multiple = 1; elif s[i] == '千': multiple = 1000; result += value*multiple; value = 0; multiple = 1; elif s[i] == '百': multiple = 100; result += value*multiple; value = 0; multiple = 1; #十这个数字,即可以作为本数,也可以作为倍数 elif s[i] == '十': if value == 0: value = 10; multiple = 1; result += value*multiple; value = 0; else: multiple = 10; result += value*multiple; value = 0; multiple = 1; else: for j in range(10): if s[i] == chineseOfNumber[j]: value = j; multiple = 1; if i >= lengthOfStr-1: result += value * multiple; result += wanPart + yiPart; #print('{0} {1}'.format(s, result)); return result;
<span style="font-size:18px;">>>> 6.5 读作 六点五 0.04 读作 零点零四 6.72 读作 六点七二 0.058 读作 零点零五八 340.09 读作 三百四十点零九 三百点七一 写作 300.71 五点零六 写作 5.06 零点零八九 写作 0.089 def readNumber(): a = [6.5,0.04,6.72,0.058,340.09]; for i in range(len(a)): print('{0} 读作 {1}'.format(a[i], floatNumberRead(a[i]))); def writeNumber(): a = ['三百点七一', '五点零六', '零点零八九']; for i in range(len(a)): print('{0} 写作 {1}'.format(a[i], floatNumberWrite(a[i]))); if __name__ == '__main__': readNumber(); writeNumber();</span>
<span style="font-size:18px;">>>> 29.5 读作 二十九点五 0.84 读作 零点八四 1.2 读作 一点二 1.8 读作 一点八 8844.43 读作 八千八百四十四点四三</span>
<span style="font-size:18px;">>>> 零点五五七 写作 0.557 四万零七十五点六九 写作 40075.69 十四点八五九 写作 14.859 九十九点七九 写作 99.79</span>
<span style="font-size:18px;">>>> 0.358, 0.385, 0.538, 0.583, 0.835, 0.853, 符合条件(1)的总共有6个数。 8.035, 8.053, 8.305, 8.503, 符合条件(2)的总共有4个数。 30.58 读作 三十点五八 30.85 读作 三十点八五 50.38 读作 五十点三八 50.83 读作 五十点八三 80.35 读作 八十点三五 80.53 读作 八十点五三 符合条件(3)的总共有6个数。 def tmp(): a = [3, 0, 5, 8]; p = perm(a); size = len(p); set1 = set([]); set2 = set([]); for i in range(size): #最后一位小数不为0 if (p[i][3] != 0): num = p[i][0] + 0.1*p[i][1] + 0.01*p[i][2]+0.001*p[i][3]; set1.add(round(num,3)); list1 = list(set1); list1.sort(); count = 0; for i in range(len(list1)): if (list1[i] < 1): print(list1[i], end = ', '); count+=1; print('符合条件(1)的总共有{0}个数。'.format(count)); count = 0; for i in range(len(list1)): if (list1[i] > 8): print(list1[i], end = ', '); count+=1; print('符合条件(2)的总共有{0}个数。'.format(count)); for i in range(size): if (p[i][0] != 0 and p[i][3] != 0): num = p[i][0]*10 + p[i][1] + 0.1*p[i][2]+0.01*p[i][3]; set2.add(round(num,2)); list2 = list(set2); list2.sort(); count = 0; for i in range(len(list2)): s = floatNumberRead(list2[i]); if (s.find('零') == -1): print('{0} 读作 {1}'.format(list2[i], s)); count+=1; print('符合条件(3)的总共有{0}个数。'.format(count));</span>
<span style="font-size:18px;">>>> [2.84, 2.88, 2.93, 3.05] def tmp(): list1 = list([3.05, 2.84, 2.88, 2.93]); list1.sort(); print(list1);</span>
<span style="font-size:18px;">def tmp(): list1 = list([3.05, 2.84, 2.88, 2.93]); list1.sort(); print(list1); list1.reverse(); print(list1);</span>
本节到此结束,欲知后事如何,请看下回分解。