• JS/Java/Python格式化金额


    //java代码
    public static void main(String[] args) {
        DecimalFormat myformat = new DecimalFormat();
        myformat.applyPattern("##,###.000");
        System.out.println(myformat.format(11112345.12345));
    }
    
    #python
    import math
    def money_format(value):
        # value = "%.2f" % float(value)
        if value.replace('.','').isdigit():
            value = '%.2f' % round(float(value), 2)
            components = str(value).split('.')
            if len(components) > 1:
                left, right = components
                right = '.' + right
            else:
                left, right = components[0], ''
    
            result = ''
            while left:
                result = left[-3:] + ',' + result
                left = left[:-3]
            return result.strip(',') + right
        else:
            return '输入的字符飞非法!!!'
            
    
    //js(来自:http://www.cnblogs.com/leejersey/p/5161207.html)
    function money_format(number){
            number = number.replace(/\,/g, "");
            if(isNaN(number) || number == "")return "";
            number = Math.round(number * 100) / 100;
            if (number < 0)
                return '-' + outputdollars(Math.floor(Math.abs(number) - 0) + '') + outputcents(Math.abs(number) - 0);
            else
                return outputdollars(Math.floor(number - 0) + '') + outputcents(number - 0); 
        }
        //格式化金额
        function outputdollars(number) {
            if (number.length <= 3)
                return (number == '' ? '0' : number);
            else {
                var mod = number.length % 3;
                var output = (mod == 0 ? '' : (number.substring(0, mod)));
                for (i = 0; i < Math.floor(number.length / 3); i++) {
                    if ((mod == 0) && (i == 0))
                        output += number.substring(mod + 3 * i, mod + 3 * i + 3);
                    else
                        output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
                }
                return (output);
            }
        }
        function outputcents(amount) {
            amount = Math.round(((amount) - Math.floor(amount)) * 100);
            return (amount < 10 ? '.0' + amount : '.' + amount);
        }
  • 相关阅读:
    人们常说的带宽是什么意思?
    关注前端性能
    单测学习笔记
    基于 Istanbul 生成测试用例覆盖率报告
    如何做高水科研
    Human-like Controllable Image Captioning with Verb-specific Semantic Roles(具有动词语义角色的类人可控图像字幕生成)
    Netty应用程序的全部基本构建模块_netty学习笔记(2)-20210405
    异步和事件驱动_netty学习笔记(1)-20210330
    理解 cosocket(转)
    nginx lua阶段处理流程
  • 原文地址:https://www.cnblogs.com/kongxiaoshuang/p/9833707.html
Copyright © 2020-2023  润新知