zoukankan      html  css  js  c++  java
  • 金额元格式化

    mfc版本的非标准元金额转换成标准元格式金额

    CString formatYuan(const char* yuanAmount)
    {
        CString fmtAmt = yuanAmount;
        fmtAmt.TrimRight();
        fmtAmt.TrimLeft();
      int strLen = fmtAmt.GetLength();
        if( strLen == 0 || fmtAmt == "-" || fmtAmt == "." || fmtAmt == "-." ){return "0.00";
        }
    
        bool isInvalid = false;
        TCHAR ch;
        short dotCount = 0;
        short ltZeroCount = 0;
        for(int i=0; i<strLen; i++)
        {
            ch = fmtAmt.GetAt(i);
            if( ch == '-' ){
                if( ++ltZeroCount > 1 ){
                    isInvalid = true;break;
                }else{
                    continue;
                }
            }
            if( ch == '.'  ){
                if( ++dotCount > 1 ){
                    isInvalid = true;
                    break;
                }else{
                    continue;;
                }
            }
            if( ch >= '0' && ch <= '9' ){
                
            }
            else{            
                isInvalid = true;
                break;
            }
        }
        if(isInvalid || (ltZeroCount == 1 && fmtAmt.GetAt(0) != '-') ){return fmtAmt;
        }
    
        ch = 0;
        if( fmtAmt.GetAt(0) == '-' ){
            ch = '-';
            fmtAmt = fmtAmt.Right(fmtAmt.GetLength()-1);
        }
            
        int dotIdx = fmtAmt.Find('.');
        if( dotIdx == -1 ){// cant't find dot
            if( fmtAmt.GetLength() > 1 ){
                fmtAmt.TrimLeft('0');
                if( fmtAmt.GetLength() == 0 ) fmtAmt = "0";
            }
            if( ch != 0 ){
                fmtAmt.Insert(0,ch);
            }
            fmtAmt += ".00";
        }
        else{
            CString dotBefore = fmtAmt.Left(dotIdx);
            CString dotAfter = fmtAmt.Right(fmtAmt.GetLength()-dotIdx-1);switch(dotBefore.GetLength())
            {
            case 0:
                dotBefore = "0";
                break;
            case 1:
                break;
            default:
                dotBefore.TrimLeft('0');
                if( dotBefore.GetLength() == 0 ) dotBefore = "0";break;
            }
    
            switch(dotAfter.GetLength())
            {
            case 0:
                dotAfter = "00";
                break;
            case 1:
                dotAfter += "0";
                break;
            default:
                dotAfter = dotAfter.Left(2);
                break;
            }
    
            fmtAmt = dotBefore + '.' + dotAfter;
            if(ch!=0){
                fmtAmt.Insert(0,ch);
            }
        }
    if( fmtAmt == "-0.00" ){
            fmtAmt = "0.00";
        }return fmtAmt;
    }
  • 相关阅读:
    项目实战
    bootscript/javascript组件
    html5应用程序标签
    bootstrap框架应用
    bootstrap javascript插件部分的笔记整理
    bootstrap页面模板
    redis安装
    nginx + vsftpd 搭建 图片服务器
    centOs7 安装
    单链表的最装逼写法
  • 原文地址:https://www.cnblogs.com/zhangmo/p/15701947.html
Copyright © 2011-2022 走看看