大写金额转换

类别:编程语言 点击:0 评论:0 推荐:

    最近做一个数据库,要用到大写的金额格式显示,于是自己写了一个,希望大家多提宝贵意见!(真是感到了C++ Builder的资料短缺了,连现成的这个函数都找不到!)

AnsiString CurrToChnNum(float CurrNum, bool DisplayZero = true, int SpaceNum = 2);

AnsiString CurrToChnNum(float CurrNum, bool DisplayZero, int SpaceNum)
//参数说明:CurrNum       这个是要转化的数值
       DisplayZero   是否要显示零 如:(显示零)壹佰 零拾 叁元 伍角 伍分
                   SpaceNum      字符间的空格数,默认值为2
{
    //数值转换为大写金额
    //18个数字单位
    const String Dw[18] = {"分","角","元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟","兆","拾","佰","仟"};
    //10个数码
    const String Num[10] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
    AnsiString sTemp;
    sTemp = FormatFloat("0.00", CurrNum);     //格式化数值
    sTemp.Delete(sTemp.Pos("."), 1);          //去掉小数点
    if ( sTemp.Length() > 18 )                //是否超长?
    {
        MessageDlg("数值位数太多,不能处理!",mtError,TMsgDlgButtons() << mbYes,0);
        return NULL;
    }
    AnsiString sRusult = "¥ ";               //字串的头部字符,根据喜好
    for (int i = sTemp.Length(); i > 0; i--)  //开始加工
    {
        if ( !DisplayZero && i > 2 && sTemp[sTemp.Length() - i + 1] - 48 == 0 )
            continue;
        sRusult += Num[sTemp[sTemp.Length()-i+1]-48];
        sRusult += Dw[i-1];
        sRusult += AnsiString::StringOfChar(' ', SpaceNum);
    }
    return sRusult;
}
//---------------------------------------------------------------------------

本文地址:http://com.8s8s.com/it/it29732.htm