浮点数字转为中文人民币显示、小数朗读字符串
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__
//
// 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__
相关文章推荐
- 将一串数字转化为中文字符串,使用 C# 将数字转换成大写人民币 (例如:用户输入2300,系统就会显示两千三百元 )
- 在使用Gson的toJson()方法转为json字符串的时候无法显示中文和符号的问题(已解决)
- 数字字符串转为相应的浮点数
- 浮点数字转为大写人民币读法---来自《疯狂Java讲义》第四章的数组应用
- 真正完整无误的pb11.5 数字(可小数)转人民币中文函数
- 中英文字符串,中文字符和英文字符转为unicode的16进制并且转为对应10进制数字
- Android开发之数字字符串小数为0的直接显示整数工具
- 真正完整无误的pb11.5 数字(可小数)转人民币中文函数
- 数字转为中文数字,数字四舍五入,字符串转大写,字符串转Map--封装类
- 字符串转为小数 保留两位数字
- Java 数字转为可朗读中文的函数
- Java将字符串格式的百分数转为小数
- PHP 5.4 中经 htmlspecialchars 转义后的中文字符串为空,DeDeCMS在PHP5.4下编辑器中文不显示问题
- python测试字符串是否包含中文,大小写字符,数字等
- HTML中文字太长的时候自动隐藏超长的字符串,直接显示:点点点
- 阿拉伯数字转为中文读法
- 编写一个函数,将一个数字字符串转换成这个字符串对应的数字(包括正浮点数、负浮点数);函数原型:double my_atof(char *str)
- 有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数。
- GradeView中,数字转换成中文显示
- 是否以某字符串结尾 是否以某字符串开始 是否是整数 裁减字符串空格 是否是浮点数 是否所有字符为数字类型 是否为空 是否是EMAIL 是否是电话号码 身份证号码验证-支持新的带x身份证 日期验证