首页 >后端开发 >Python教程 >把大数据数字口语化

把大数据数字口语化

高洛峰
高洛峰原创
2016-10-18 13:28:281108浏览

当出现万以上的整型数字时,经常要把它们口语化比较直观。下面分享两段代码,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 = parseInt(num/10000) + &#39;万&#39; + num % 10000;
            }else{
                ret = parseInt(num/10000) + &#39;万&#39;;
            }
        }else{
            n2 = num%Math.pow(10,8);
            if (n2%10000 != 0 & n2/10000 != 0) {
                ret = parseInt(num/Math.pow(10,8)) + &#39;亿&#39; + parseInt(n2/10000) + &#39;万&#39; + (n2%10000);
            }else if(n2%10000 != 0 & n2/10000 == 0){
                ret = parseInt(num/Math.pow(10,8)) + &#39;亿&#39; +  parseInt(n2%10000);
            }else if(n2%10000 == 0 & n2/10000 != 0){
                ret = parseInt(num/Math.pow(10,8)) + &#39;亿&#39; +  parseInt(n2/10000) + &#39;万&#39;;
            }else if(n2%10000 == 0 & n2/10000 == 0){
                ret = (num/Math.pow(10,8)) + &#39;亿&#39;;
            }
        }
    }
    return ret
}


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn