日期:2014-05-16  浏览次数:20387 次

数字代码把大数据数字口语化(python+js) 数字代码

首先声明,我是一个菜鸟。一下文章中涌现技术误导情况盖不负责

????当涌现万以上的整型数字时,经常要把它们口语化比拟直观。面下分享两段代码,python与js的

????python

def fn(num):
    '''
    把数字口语化
    '''
    
    ret = ''
    num = int(num)
    if num/10000 == 0:
        ret = str(num)
    else:
        if num/10**8 == 0:
            if num%10000 != 0:
                ret = str(num/10000) + '' + str(num % 10000)
            else:
                ret = str(num/10000) + ''
        else:
            n2 = num%10**8
            if n2%10000 != 0 and n2/10000 != 0:
                ret = str(num/10**8) + '亿' + str(n2/10000) + '' + str(n2%10000)
            elif  n2%10000 != 0 and n2/10000 == 0:
                ret = str(num/10**8) + '亿' +  str(n2%10000)
            elif  n2%10000 == 0 and n2/10000 != 0:
                ret = str(num/10**8) + '亿' +  str(n2/10000) + ''
            elif  n2%10000 == 0 and n2/10000 == 0:
                ret = str(num/10**8) + '亿'
    return ret

javascript:

function int2string(num) {
    num = Number(num);
    if (num/10000 < 1){
        ret = num;
    }else{
        if (num/Math.pow(10,8) < 1) {
            if (num%10000 != 0) {
                ret