美文网首页
Python之美元大写转换器

Python之美元大写转换器

作者: Leo_23 | 来源:发表于2018-10-11 15:57 被阅读302次

Python之美元大写转换器

网上找了好久没找到python版本的,照着js抄了一下。

原文链接美元大写转换器

#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = "leo"
__time__ = "2018-10-11"

'''
http://www.52ij.com/zhuanhuanqi/meiyuandaxie/shuzi.js
改编
'''

import math

arr1 = ("", " thousand", " million", " billion")
arr2 = ("zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety")
arr3 = ("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
arr4 = ("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen")


def English(a):
    strRet = ""
    if (len(a) == 3 and a[0:3] != "000"):
        if a[0:1] != "0":
            strRet = strRet + arr3[int(a[0:1])] + " hundred"
            if a[1:2] != "00":
                strRet = strRet + " and "
        a = a[1:]
    if len(a) == 2:
        if a[0:1] == "0":
            a = a[1:]
        elif a[0:1] == "1":
            strRet = strRet + arr4[int(a[1:2])]
        else:
            strRet = strRet + arr2[int(a[0:1])]
            if a[1:1] != "0":
                strRet = strRet + "-"
            a = a[1:]
    if (len(a) == 1 and a[0:1] != "0"):
        strRet = strRet + arr3[int(a[0:1])]
    return strRet


def doToEn(a):
    b = len(a)
    f, h = 0, 0
    g = ""
    e = math.ceil(b / 3)
    k = b - e * 3
    g = ""

    for f in range(k, b, 3):
        h += 1
        if f >= 0:
            num3 = a[f:f + 3]
        else:
            num3 = a[0:k + 3]
        strEng = English(num3)
        if strEng != "":
            if g != "":
                g += ","
            g += English(num3) + arr1[e - h]

    return "U.S. DOLLARS " + g.upper() + " ONLY"


def doToEnWuxiaoshudian(a):
    b = len(a)
    f, h = 0, 0
    g = ""
    e = math.ceil(b / 3)
    k = b - e * 3
    g = ""

    for f in range(k, b, 3):
        h += 1
        if f >= 0:
            num3 = a[f:f + 3]
        else:
            num3 = a[0:k + 3]
        strEng = English(num3)
        if strEng != "":
            if g != "":
                g = g + ","
            g = g + English(num3) + arr1[e - h]

    return "U.S. DOLLARS " + g.upper()


def doToEnYouxiaoshudian(a):
    b = len(a)
    f, h = 0, 0
    g = ""
    e = math.ceil(b / 3)
    k = b - e * 3
    g = ""

    for f in range(k, b, 3):
        h += 1
        if f >= 0:
            num3 = a[f:f + 3]
        else:
            num3 = a[0:k + 3]
        strEng = English(num3)
        if strEng != "":
            if g != "":
                g = g + ","
            g = g + English(num3) + arr1[e - h]

    return "CENTS " + g.upper() + " ONLY"


if __name__ == '__main__':
    number = '1234.56'
    if str.find(number, '.') != -1:
        # 有小数点
        aaaaa = number.split(".")[0]
        bbbbb = number.split(".")[1]
        ccccc = doToEnWuxiaoshudian(aaaaa)
        ddddd = ""
        if len(bbbbb) > 2:
            print("请精确到小数点后两位!如:6324.38")
        else:
            ddddd = doToEnYouxiaoshudian(bbbbb)
        result = ccccc + " AND " + ddddd

    else:
        result = doToEn(number)

    print(result)

相关文章

  • Python之美元大写转换器

    Python之美元大写转换器 网上找了好久没找到python版本的,照着js抄了一下。 原文链接美元大写转换器

  • Python ☞ day 8

    Python学习笔记之 面向对象 第一个python类 设计类类名:见名之意,首字母大写,其他遵循驼峰原则属性:见...

  • Erlang学习笔记--变量与原子

    变量 变量都是大写开头 在 erlang 中,变量都是大写开头,如X, Name,而不像 python,java,...

  • python lower() 方法

    Python lower() 方法转换字符串中所有大写--->小写。

  • python_命名规范

    总结前人之精华,去其糟粕 Python的编码命名规则 项目名 首字母大写,其余小写单词,若多个单词组合可以添加“_...

  • Python基础篇(一)

    python基础方法与函数 title() 首字母大写 upper() 将字符串全部改写为大写字母 lower()...

  • Tkinter模块找不到,解决方法

    import Tkinter模块在 python 3.x 和 python 2.x 之间的差别: 注意:首字母大写...

  • Python 判断字符串是否为大写及 is 方法延伸

    Python 判断字符串是否为大写及延伸以下方法仅判断字符,数字和符号不影响结果isupper()判断是否都为大写...

  • alibaba Java开发手册笔记

    1.1命名风格 代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号开始。 常量命名全部大写,单词间...

  • Mac终端查看python版本号

    注意: python语言大小写非常敏感,利用Mac终端查看版本号必须用大写的python -V 另外cocoapo...

网友评论

      本文标题:Python之美元大写转换器

      本文链接:https://www.haomeiwen.com/subject/ujbbaftx.html