数字转换成大写中文

MesserP 2009-03-06 09:38:33
想将用户在textbox内输入的一个数字转换成中文大写并显示在另一个textbox内。如何实现请教各位了~~~
...全文
3006 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
止戈而立 2009-03-23
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 MesserP 的回复:]
regex出错,说上下文中不存在这个...怎么办???[/Quote]

using System.Text.RegularExpressions;
MesserP 2009-03-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 MesserP 的回复:]
引用 7 楼 min_jie 的回复:
C# code//修改一下,以便转换更大的数
string GetChineseString(long number)
{
string cStr = "零一二三四五六七八九 十百千";
string[] unitStr = new string[] { "", "万", "亿", "万亿", "兆" };
string result = string.Empty;
for (int i = 0; i < number.ToString().Length; i++)
{

[/Quote]

regex出错,说上下文中不存在这个...怎么办???
止戈而立 2009-03-07
  • 打赏
  • 举报
回复
//修改一下,以便转换更大的数
string GetChineseString(long number)
{
string cStr = "零一二三四五六七八九 十百千";
string[] unitStr = new string[] { "", "万", "亿", "万亿", "兆" };
string result = string.Empty;
for (int i = 0; i < number.ToString().Length; i++)
{
int temp = (int)((long)(number / (long)Math.Pow(10, i)) % 10);
int unit = (int)i / 4;
if (i % 4 == 0) result = unitStr[unit] + result;
result = cStr[temp] + cStr[10 + i % 4].ToString().Trim() + result;
}
result = Regex.Replace(result, "(零[十百千])+", "零");
result = Regex.Replace(result, "零{2,}", "零");
result = Regex.Replace(result, "零([万亿])", "$1").TrimEnd('零');
return result;
}
止戈而立 2009-03-07
  • 打赏
  • 举报
回复
//其实用不着写很长很长的代码,只是写了整形数的部分,小数部分也很容易实现,就不写了
using System.Text.RegularExpressions;

string GetChineseString(long number)
{
string cStr = "零一二三四五六七八九 十百千万亿";
string result = string.Empty;
for (int i = 0; i < number.ToString().Length; i++)
{
int temp = (int)((long)(number / (long)Math.Pow(10, i)) % 10);//获取第i位的数字
int unit = (int)i / 4;
if (i % 4 == 0 && unit > 0) result = cStr[13 + unit] + result;//检查是否需要加上万或亿
result = cStr[temp] + cStr[10 + i % 4].ToString().Trim() + result;
}
result = Regex.Replace(result, "(零[十百千])+", "零");//将零十零百等转为零
result = Regex.Replace(result, "零{2,}", "零");//将多个连续的零转为单个零
result = Regex.Replace(result, "零([万亿])", "$1").TrimEnd('零');//将零万零亿的零去掉
return result;
}
杏鲍菇 2009-03-07
  • 打赏
  • 举报
回复
mark
止戈而立 2009-03-07
  • 打赏
  • 举报
回复
我把转小数的方法写到博客里,有兴趣的话可以看下:
http://blog.csdn.net/min_jie/archive/2009/03/07/3964885.aspx
止戈而立 2009-03-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 MesserP 的回复:]
引用 7 楼 min_jie 的回复:
C# code//修改一下,以便转换更大的数
看不太懂。。。能帮我写成从textbox1内读数字。。写中文到textbox2中嘛。。谢谢拉~[/Quote]

//调用:
this.textBox2.Text=GetChineseString(long.Parse(this.textBox1.Text));
zhaozhijun0207 2009-03-07
  • 打赏
  • 举报
回复
http://blog.csdn.net/zhaozhijun0207/archive/2009/03/04/3957873.aspx

using System;
using System.Collections.Generic;
using System.Text;

namespace KySoft
{
public class cls转换金额
{
public cls转换金额()
{

}

public static string Get大写金额(double dblNumeric)
{
string N2S = "";
string strPlace_1 = "";
string strPlace_2 = "";
string strPlace_4 = "";
string strPlace_5 = "";
string strPlace_6 = "";
string strPlace_7 = "";
double intLen = 0;

strPlace_1 = "零壹贰叁肆伍陆柒捌玖";
strPlace_2 = "仟佰拾万仟佰拾元角分";

strPlace_4 = (dblNumeric * 100).ToString().Trim();
if (strPlace_4.Length > 10 && dblNumeric > 0 && dblNumeric < 99999999.99)
{
intLen = Convert.ToInt32(dblNumeric * 100) / 100;
if (dblNumeric - intLen >= 0.005)
{
intLen = intLen + 0.01;
}
strPlace_4 = (intLen * 100).ToString().Trim();
}

if (strPlace_4.Length > 10 || dblNumeric < 0)
N2S = "大于等于 99999999.99 的数值不能转换";
else
{
//strPlace_4 = GetFillString(10 - strPlace_4.Length," ") + strPlace_4;
strPlace_4 = strPlace_4.PadLeft(10, ' ');
N2S = "";
for (int i = 0; i < 10; i++)
{
strPlace_5 = Convert.ToString(strPlace_4[i]);
if (strPlace_5 != " ")
{
strPlace_6 = Convert.ToString(strPlace_1[Convert.ToInt32(strPlace_5)]);
strPlace_7 = Convert.ToString(strPlace_2[i]);
if (strPlace_5 == "0" && i != 3 && i != 7)
strPlace_7 = "";
if ((i == 8 && strPlace_4.Substring(i, 2) == "00") || (strPlace_5 == "0" && (i == 3 || i == 7 || i == 9)))
strPlace_6 = "";
N2S = N2S + strPlace_6 + strPlace_7;
if (Convert.ToString(strPlace_4[i]) == "0" && strPlace_4[i] != '0' && (i == 3 || i == 7))
N2S = N2S + "零";
}
}
if (strPlace_5 == "0" && N2S != "")
N2S = N2S + "整";
}
return N2S;
}
}
}




textbox2.tex=Get大写金额((int32)textbox1.text.tostring());
wuyq11 2009-03-07
  • 打赏
  • 举报
回复
代码都写了,直接用方法转化就可。txt2.text=方法(text1.text);
MesserP 2009-03-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 min_jie 的回复:]
C# code//修改一下,以便转换更大的数
string GetChineseString(long number)
{
string cStr = "零一二三四五六七八九 十百千";
string[] unitStr = new string[] { "", "万", "亿", "万亿", "兆" };
string result = string.Empty;
for (int i = 0; i < number.ToString().Length; i++)
{
int temp = (int)((long)(numbe…
[/Quote]
看不太懂。。。能帮我写成从textbox1内读数字。。写中文到textbox2中嘛。。谢谢拉~
gqchen1981 2009-03-07
  • 打赏
  • 举报
回复
mark,学习下。
haifeng39 2009-03-06
  • 打赏
  • 举报
回复
mark
oyljerry 2009-03-06
  • 打赏
  • 举报
回复
做一个数字对应中文大写的数组,然后自己对应转换.
boci 2009-03-06
  • 打赏
  • 举报
回复
http://dev.rdxx.com/NET/CSharp/2008/7/166473685586.shtml
cppfaq 2009-03-06
  • 打赏
  • 举报
回复
http://blog.csdn.net/waterboy/archive/2004/08/13/74060.aspx

 小写数字转换为中文大写的最完整C#方案 收藏
本程序用于将小写数字转换为中文大写, 提供以下功能:
1. 允许任意整数或小数, 范围在(-10,000,000,000,000,000 ~ 10,000,000,000,000,000)之间.包括"0.", ".0", "-.0", ".0-", "+114", "20.+"等.
2. 能识别并处理任何错误输入
3. 算法稳定, 速度较快, 中文大写比较符合语言习惯

using System;
using System.Text;

namespace DigitToChnText
{
///
/// 本程序用于将小写数字变成大写中文数字
/// 算法设计:黄晶
/// 程序制作:黄晶
/// 时间:2004年8月12日
///
class DigitToChnText
{
private readonly char[] chnText;
private readonly char[] chnDigit;

public DigitToChnText()
{
chnText = new char[]{'零', '一', '二', '三', '四', '五', '六', '七', '八', '九'};
chnDigit = new char[]{'十', '百', '千', '万', '亿'};
}

public string Convert( string strDigit )
{
// 检查输入数字
decimal dec;
try
{
dec = decimal.Parse( strDigit );
}
catch( FormatException )
{
throw new Exception("输入数字的格式不正确。");
}
catch( Exception e )
{
throw e;
}

if( dec <= -10000000000000000m || dec >= 10000000000000000m )
{
throw new Exception( "输入数字太大或太小,超出范围。" );
}

StringBuilder strResult = new StringBuilder();

// 提取符号部分
// '+'在最前
if( "+" == strDigit.Substring( 0, 1 ) )
{
strDigit = strDigit.Substring( 1 );
}
// '-'在最前
else if( "-" == strDigit.Substring( 0, 1 ) )
{
strResult.Append( '负' );
strDigit = strDigit.Substring( 1 );
}
// '+'在最后
else if( "+" == strDigit.Substring( strDigit.Length - 1, 1 ) )
{
strDigit = strDigit.Substring( 0, strDigit.Length - 1 );
}
// '-'在最后
else if( "-" == strDigit.Substring( strDigit.Length - 1, 1 ) )
{
strResult.Append( '负' );
strDigit = strDigit.Substring( 0, strDigit.Length - 1 );
}

// 提取整数和小数部分
int indexOfPoint;
if( -1 == ( indexOfPoint = strDigit.IndexOf('.') ) ) // 如果没有小数部分
{
strResult.Append( ConvertIntegral( strDigit ) );
}
else // 有小数部分
{
// 先转换整数部分
if( 0 == indexOfPoint ) // 如果“.”是第一个字符
{
strResult.Append( '零' );
}
else
{
strResult.Append( ConvertIntegral( strDigit.Substring( 0, indexOfPoint ) ) );
}

// 再转换小数部分
if( strDigit.Length - 1 != indexOfPoint ) // 如果“.”不是最后一个字符
{
strResult.Append( '点');
strResult.Append( ConvertFractional( strDigit.Substring( indexOfPoint + 1 ) ) );
}
}

return strResult.ToString();
}

// 转换整数部分
protected string ConvertIntegral(string strIntegral)
{
// 去掉数字前面所有的'0'
// 并把数字分割到字符数组中
char[] integral = ( ( long.Parse( strIntegral ) ).ToString() ).ToCharArray();

// 变成中文数字并添加中文数位
StringBuilder strInt = new StringBuilder();

int i;
int digit;
digit = integral.Length - 1;

// 处理最高位到十位的所有数字
for( i = 0; i < integral.Length - 1; i++ )
{
strInt.Append( chnText[ integral[i] - '0'] );

if( 0 == digit % 4 ) // '万' 或 '亿'
{
if( 4 == digit || 12 == digit )
{
strInt.Append( chnDigit[3] ); // '万'
}
else if( 8 == digit )
{
strInt.Append( chnDigit[4] ); // '亿'
}
}
else // '十','百'或'千'
{
strInt.Append( chnDigit[digit % 4 - 1] );
}

digit--;
}

// 如果个位数不是'0'
// 或者个位数为‘0’但只有一位数
// 则添加相应的中文数字
if( '0' != integral[ integral.Length - 1 ] || 1 == integral.Length )
{
strInt.Append( chnText[ integral[ i ] - '0' ] );
}

// 遍历整个字符串
i = 0;
while( i < strInt.Length)
{
int j = i;

bool bDoSomething = false;

// 查找所有相连的“零X”结构
while( j < strInt.Length - 1 && "零" == strInt.ToString().Substring( j, 1 ) )
{
string strTemp = strInt.ToString().Substring( j + 1, 1 );

// 如果是“零万”或者“零亿”则停止查找
if( "万" == strTemp || "亿" == strTemp )
{
bDoSomething = true;
break;
}

j += 2;
}

if( j != i) // 如果找到“零X”结构,则全部删除
{
strInt = strInt.Remove( i, j - i);

// 除了在最尾处,或后面不是"零万"或"零亿"的情况下,
// 其他处均补入一个“零”
if( i <= strInt.Length - 1 && !bDoSomething )
{
strInt = strInt.Insert( i, '零' );
i++;
}
}

if( bDoSomething ) // 如果找到"零万"或"零亿"结构
{
strInt = strInt.Remove( i, 1 ); // 去掉'零'
i++;
continue;
}

// 指针每次可移动2位
i += 2;
}

// 遇到“亿万”变成“亿零”或"亿"
int index = strInt.ToString().IndexOf( "亿万" );
if( -1 != index )
{
if( strInt.Length - 2 != index && // 如果"亿万"不在最后
( index + 2 < strInt.Length && "零" != strInt.ToString().Substring( index + 2, 1) ) ) // 并且其后没有"零"
strInt = strInt.Replace( "亿万", "亿零", index, 2 );
else
strInt = strInt.Replace( "亿万", "亿", index, 2);
}

// 开头为“一十”改为“十”
if( strInt.Length > 1 && "一十" == strInt.ToString().Substring( 0, 2 ) )
{
strInt = strInt.Remove( 0, 1 );
}

return strInt.ToString();
}

// 转换小数部分
protected string ConvertFractional( string strFractional )
{
char[] fractional = strFractional.ToCharArray();
StringBuilder strFrac = new StringBuilder();

// 变成中文数字
int i;
for( i = 0; i < fractional.Length; i++ )
{
strFrac.Append( chnText[ fractional[ i ] - '0' ] );
}

return strFrac.ToString();
}

///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
DigitToChnText obj = new DigitToChnText();

string str = " ";
while( "" != str )
{
Console.Write( "请输入小写数字: " );
if( "" == ( str = Console.ReadLine() ) )
break;

try
{
Console.WriteLine( "大写中文数字为: {0}\n", obj.Convert( str ) );
}
catch( Exception e )
{
Console.WriteLine( "错误: {0}\n", e.Message );
continue;
}
}
}
}
}

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧