想将用户在textbox内输入的一个数字转换成中文大写并显示在另一个textbox内。如何实现请教各位了~~~
解决方案 »
- 【Linq】多个集合的结果合并后遍历
- 在C#中如何实现蚂蚁线啊!多谢!
- C#显示不了窗体,报告这个错误,请问如何解决?
- SQL高手请进,解决了马上散分
- 如何判断一字符串("0.123",".123","123,456,789.0","-.123","+0.123",“1.46285150E-07”或“1.46026710E+07”等)是否为有效数值????
- ●○●○怎么把dataset里面的数据传给一个数组并且返回数组●○●○请各位大侠帮忙了!!
- Socket在UDP协议下,怎么接收无链接的非确定大小数据呀??
- OpenText Document Management是否有C#的开发接口
- c#读写txt文件的问题
- 如何使DataGrid的每个Cell可编辑
- 请问为什么MarshalByRefObject派生类不能使用属性?
- 紧急求助!C#调用word绘表格
本程序用于将小写数字转换为中文大写, 提供以下功能:
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;
}
}
}
}
}
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;
}
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;
}
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());
//调用:
this.textBox2.Text=GetChineseString(long.Parse(this.textBox1.Text));
http://blog.csdn.net/min_jie/archive/2009/03/07/3964885.aspx
regex出错,说上下文中不存在这个...怎么办???
using System.Text.RegularExpressions;