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;
    }
  • 相关阅读:
    npm start 时报 node_modules/nan 错误
    webpack 插件依赖
    reducer-form 数组字段 在removeField/removeField 之后 dirty 不改变的问题
    sass 入门教程
    解决国内gem不能用的问题
    nrm 快速切换 NPM 源
    数值补全指定位数
    文本溢出使用省略号
    css/js效果笔记
    用MyEclipse,只要一开css文件就报错,按下任何键都报错
  • 原文地址:https://www.cnblogs.com/zhangmo/p/15701947.html
Copyright © 2011-2022 走看看