• 金额元格式化


    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;
    }
  • 相关阅读:
    properties文件不能输入中文
    java: bin里面的.class文件没有了怎么办
    LINUX 系统java自动化启动浏览器 提示:The driver is not executable: /home/pt/Downloads/googledriver/chromedriver_linux64/chromedriver
    MarkdownTest
    洛谷P5364 [SNOI2017]礼物 题解
    长链剖分
    左偏树(可并堆)
    Splay
    分层图最短路
    整体二分
  • 原文地址:https://www.cnblogs.com/zhangmo/p/15701947.html
Copyright © 2020-2023  润新知