您的位置:首页 > 其它

浮点数字转为中文人民币显示、小数朗读字符串

2006-05-20 03:10 621 查看
//////////////////////////////////////////////////////////////////////
//
// TFX 中文处理库 V1.0.0
//
// Copyright (c) 2006 TRUEWAY(TM). All rights reserved.
//
// E-Mail: truewaylee@163.com
// MSN: truewaylee@hotmail.com
//
//--------------------------------------------------------------------
// 版本历史:
//--------------------------------------------------------------------
//
// V0.xx 1999-2002 Delphi 版 TfxMakeChnCurrency
// V1.0.0 2006.01.13 创建初始版本, 编制 TfxChs 类
//
//////////////////////////////////////////////////////////////////////

#if !defined(__TFX__CHS__)
#define __TFX__CHS__

#pragma once

#include "tfx.h"

#define TFX_CHS_GET_CURRENCY (0)
#define TFX_CHS_GET_FLOAT (1)

#define TFX_CHS_MAX_VALUE (1000000000000.00 - 0.01)
#define TFX_CHS_MIN_VALUE (-1000000000000.00 + 0.01)

class CTfxChs
{
public:

// mode == TFX_CHS_GET_CURRENCY, 生成人民币格式
// mode == TFX_CHS_GET_FLOAT, 生成普通小数格式
// 能够翻译的最大值 +999999999999.99
// 能够翻译的最小值 -999999999999.99

static int GetCurrencyStr(double AVar, string& ret_str,
int mode = TFX_CHS_GET_CURRENCY);

private:
static string GetPartStr(const string Str);
static string ChnNumNames(char num) ;
};

inline string CTfxChs::ChnNumNames(char num)
{
char CONST_ChnNumNames[10][30] =
{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};

int index = num - '0';

return CONST_ChnNumNames[index];

}

// 分析每一段
inline string CTfxChs::GetPartStr(const string Str)
{
int I = 0, Index = string::npos;

for (I = 0; I < 4; I ++)// 找到第一个非"0"的字符
{
if (Str[I] != '0' )
{
Index = I;
break;
}

}
if (Index == string::npos ) return "";

string Result = "";
for (I = Index; I < 4; I ++)
{
switch(I)
{
case 0: // 千位
{
Result = Result + ChnNumNames(Str[0]) + "仟";
break;
}
case 1: // 百位
{
if (Str[1] > '0')
Result = Result + ChnNumNames(Str[1]) + "佰";
else
if (Str[2] != '0' ) Result = Result + "零";

break;
}
case 2: // 十位
{
if (Str[2] > '0' )
{
if (Str[2] > '1' )
Result = Result + ChnNumNames(Str[2]); // "10" 不译成 "壹拾"

Result = Result + "拾";
}
else
if (Str[3] != '0') Result = Result + "零";

break;

}

case 3:
{
if (Str[3] > '0' ) Result = Result + ChnNumNames(Str[3]);

break;
}

default:
break;
}

}
return Result;
}

inline int CTfxChs::GetCurrencyStr(double AVar, string& ret_str, int mode)
{

string Part1, Part2, Part3, Part4, VarStr;
int I = 0;
string Result = "";

// ===================================================================
// 翻译整数部分
// ===================================================================
char buff[200] = {0};

if ( AVar < 0 )
sprintf(buff, "%18.2f", - AVar);
else
sprintf(buff, "%18.2f", AVar);

VarStr = TfxTrim(buff);

if ( (AVar > TFX_CHS_MAX_VALUE) || (AVar < TFX_CHS_MIN_VALUE ) )
{ // 越界时不翻译, 直接返回
ret_str = VarStr;
return -1 ;
}

for( I = VarStr.length() ; I <= 14; I ++) VarStr = "0" + VarStr;

// 先获得每个部分
Part1 = TfxMid(VarStr, 0, 4);
Part2 = TfxMid(VarStr, 4, 4);
Part3 = TfxMid(VarStr, 8, 4);
Part4 = TfxMid(VarStr, 13, 2);

Result = Result + GetPartStr(Part1);
if (Result != "") Result = Result + "亿";

////
if ( (Part2[0] == '0') && (Result != "") &&
((TfxStrToInt(Part2) != 0) || // 整个区不为 0 时始终翻译
// 整个区为 0 时, 则低区不为 0 时显示
((TfxStrToInt(Part2) == 0) && ((TfxStrToInt(Part3) != 0) || (TfxStrToInt(Part4) != 0)))
) )
Result = Result + "零";

Result = Result + GetPartStr(Part2);
if ( (Result != "") && (TfxStrToInt(Part2) > 0) ) Result = Result + "万";

////
if ( (Part3[0] == '0') && (Result != "") && (TfxStrToInt(Part3) != 0)
&& (TfxStrToInt(Part2) != 0 ) )
Result = Result + "零";

Result = Result + GetPartStr(Part3);

// ===================================================================
// 翻译小数部分
// ===================================================================
if ( mode == TFX_CHS_GET_CURRENCY )
{
// ===================================================================
// 按人民币的格式
// ===================================================================
if (Result != "" )
Result = Result + "元";
else
if (TfxStrToInt(Part4) == 0 ) Result = Result + "零元";

////
if ( (TfxStrToInt(Part3) == 0) && (Result != "")
&& (TfxStrToInt(Part4) != 0) && (Part4[0] != '0') ) Result = Result + "零";

if (Part4[0] > '0')
Result = Result + ChnNumNames(Part4[0]) + "角";
else
if ( (Result != "") && (Part4[1] != '0') ) Result = Result + "零";

if (Part4[1] > '0')
Result = Result + ChnNumNames(Part4[1]) + "分";
else
if ( Result != "" ) Result = Result + "整";

if ( AVar < 0 )
Result = "负" + Result;
}
else
{
// ===================================================================
// 按小数格式
// ===================================================================
if (Result != "" )
{
// 整数位不为0
if (TfxStrToInt(Part4) > 0 ) Result = Result + "点";
}
else
{
// 整数位为0
if (TfxStrToInt(Part4) > 0 )
Result = Result + "零点";
else
{
ret_str = "零";
return 0;
}
}

if (Part4[0] > '0' )
// 小数点第一位不为0
Result = Result + ChnNumNames(Part4[0]);
else
// 小数点第一位为0, 且小数区不为0
if ( (Result != "") && (Part4[1] != '0') ) Result = Result + "零";

if ( Part4[1] > '0' )
// 小数点第二位不为0
Result = Result + ChnNumNames(Part4[1]);

if (AVar < 0 ) Result = "负" + Result;
}

ret_str = Result;

return 0;

}

#endif // __TFX__CHS__
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐