A:简单点,格式比较严格
private
float
getMoney(
string
strBig)
{
string strExp = "" ;
string [] arrBig = new string [] { " 整 " , "" , " 零 " , " +0 " , " 壹 " , " +1 " , " 贰 " , " +2 " , " 叁 " , " +3 " , " 肆 " , " +4 " , " 伍 " , " +5 " , " 陆 " , " +6 " , " 柒 " , " +7 " , " 捌 " , " +8 " , " 玖 " , " +9 " , " 分 " , " *0.01 " , " 角 " , " *0.1 " , " 元 " , " *1 " , " 拾 " , " *10.00 " , " 佰 " , " *100.00 " , " 仟 " , " *1000.00 " , " 万 " , " *10000.00 " , " 亿 " , " *100000000.00 " } ;
for ( int i = 0 ; i < strBig.Length; i ++ )
{
for ( int j = 0 ; j < arrBig.Length - 1 ;j += 2 )
{
if (arrBig[j] == strBig[i].ToString())
{
strExp += arrBig[j + 1 ].ToString();
continue ;
}
}
}
object ret = new DataTable().Compute(strExp, "" );
return float .Parse(ret.ToString());
// Console.WriteLine(getMoney("玖仟零贰拾壹元陆角肆分"));
// Console.WriteLine(getMoney("玖仟万亿零贰拾壹元陆角肆分"));
}
{
string strExp = "" ;
string [] arrBig = new string [] { " 整 " , "" , " 零 " , " +0 " , " 壹 " , " +1 " , " 贰 " , " +2 " , " 叁 " , " +3 " , " 肆 " , " +4 " , " 伍 " , " +5 " , " 陆 " , " +6 " , " 柒 " , " +7 " , " 捌 " , " +8 " , " 玖 " , " +9 " , " 分 " , " *0.01 " , " 角 " , " *0.1 " , " 元 " , " *1 " , " 拾 " , " *10.00 " , " 佰 " , " *100.00 " , " 仟 " , " *1000.00 " , " 万 " , " *10000.00 " , " 亿 " , " *100000000.00 " } ;
for ( int i = 0 ; i < strBig.Length; i ++ )
{
for ( int j = 0 ; j < arrBig.Length - 1 ;j += 2 )
{
if (arrBig[j] == strBig[i].ToString())
{
strExp += arrBig[j + 1 ].ToString();
continue ;
}
}
}
object ret = new DataTable().Compute(strExp, "" );
return float .Parse(ret.ToString());
// Console.WriteLine(getMoney("玖仟零贰拾壹元陆角肆分"));
// Console.WriteLine(getMoney("玖仟万亿零贰拾壹元陆角肆分"));
}
伴水兄的这个较狠
public
double
DecodeMoneyCn(
string
AText)
{
AText = AText.Replace( " 万万 " , " 亿 " );
AText = AText.Replace( " 点 " , " 元 " );
AText = AText.Replace( " 块 " , " 元 " );
AText = AText.Replace( " 毛 " , " 角 " );
double vResult = 0 ;
double vNumber = 0 ; // 当前数字
double vTemp = 0 ;
int vDecimal = 0 ; // 是否出现小数点
foreach ( char vChar in AText)
{
int i = " 零一二三四五六七八九 " .IndexOf(vChar);
if (i < 0 ) i = " 零幺两三四五六七八九 " .IndexOf(vChar);
if (i < 0 ) i = " 零壹贰叁肆伍陆柒捌玖 " .IndexOf(vChar);
if (i > 0 )
{
vNumber = i;
if (vDecimal > 0 )
{
vResult += vNumber * Math.Pow( 10 , - vDecimal);
vDecimal ++ ;
vNumber = 0 ;
}
}
else
{
i = " 元十百千万亿 " .IndexOf(vChar);
if (i < 0 ) i = " 整拾佰仟万亿 " .IndexOf(vChar);
if (i == 5 ) i = 8 ;
if (i > 0 )
{
if (i >= 4 )
{
vTemp += vNumber;
if (vTemp == 0 ) vTemp = 1 ;
vResult += vTemp * Math.Pow( 10 , i);
vTemp = 0 ;
}
else vTemp += vNumber * Math.Pow( 10 , i);
}
else
{
i = " 元角分 " .IndexOf(vChar);
if (i > 0 )
{
vTemp += vNumber;
vResult += vTemp * Math.Pow( 10 , - i);
vTemp = 0 ;
}
else if (i == 0 )
{
vTemp += vNumber;
vResult += vTemp;
vDecimal = 1 ;
vTemp = 0 ;
}
}
vNumber = 0 ;
}
}
return vResult + vTemp + vNumber;
}
private void button2_Click( object sender, EventArgs e)
{
string [] vTestText = {
" 十二点五六 " ,
" 一亿零一万零五 " ,
" 四万万 " ,
" 九十八亿七千六百五十四万三千二百一十 " ,
" 五元一角四分 " , " 壹佰元整 " ,
" 三千五百万 " ,
" 九块二毛 " } ;
foreach ( string vText in vTestText)
{
Console.WriteLine( " DecodeMoneyCn("{0}")={1} " , vText,
DecodeMoneyCn(vText));
}
}
{
AText = AText.Replace( " 万万 " , " 亿 " );
AText = AText.Replace( " 点 " , " 元 " );
AText = AText.Replace( " 块 " , " 元 " );
AText = AText.Replace( " 毛 " , " 角 " );
double vResult = 0 ;
double vNumber = 0 ; // 当前数字
double vTemp = 0 ;
int vDecimal = 0 ; // 是否出现小数点
foreach ( char vChar in AText)
{
int i = " 零一二三四五六七八九 " .IndexOf(vChar);
if (i < 0 ) i = " 零幺两三四五六七八九 " .IndexOf(vChar);
if (i < 0 ) i = " 零壹贰叁肆伍陆柒捌玖 " .IndexOf(vChar);
if (i > 0 )
{
vNumber = i;
if (vDecimal > 0 )
{
vResult += vNumber * Math.Pow( 10 , - vDecimal);
vDecimal ++ ;
vNumber = 0 ;
}
}
else
{
i = " 元十百千万亿 " .IndexOf(vChar);
if (i < 0 ) i = " 整拾佰仟万亿 " .IndexOf(vChar);
if (i == 5 ) i = 8 ;
if (i > 0 )
{
if (i >= 4 )
{
vTemp += vNumber;
if (vTemp == 0 ) vTemp = 1 ;
vResult += vTemp * Math.Pow( 10 , i);
vTemp = 0 ;
}
else vTemp += vNumber * Math.Pow( 10 , i);
}
else
{
i = " 元角分 " .IndexOf(vChar);
if (i > 0 )
{
vTemp += vNumber;
vResult += vTemp * Math.Pow( 10 , - i);
vTemp = 0 ;
}
else if (i == 0 )
{
vTemp += vNumber;
vResult += vTemp;
vDecimal = 1 ;
vTemp = 0 ;
}
}
vNumber = 0 ;
}
}
return vResult + vTemp + vNumber;
}
private void button2_Click( object sender, EventArgs e)
{
string [] vTestText = {
" 十二点五六 " ,
" 一亿零一万零五 " ,
" 四万万 " ,
" 九十八亿七千六百五十四万三千二百一十 " ,
" 五元一角四分 " , " 壹佰元整 " ,
" 三千五百万 " ,
" 九块二毛 " } ;
foreach ( string vText in vTestText)
{
Console.WriteLine( " DecodeMoneyCn("{0}")={1} " , vText,
DecodeMoneyCn(vText));
}
}