java人民币大小写转换_人民币大小写转换
usingSystem;usingSystem.Text;usingSystem.Text.R
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace HKH.Common
{
///
/// 人民幣大小寫格式轉(zhuǎn)換
///
/// Create By Lwt on 2006/09/23
///
public class clsRMB
{
private clsRMB()
{
}
#region 格式化
///
/// 格式化(大寫轉(zhuǎn)小寫)
///
///
///
public static double Format(string strRMB)
{
try
{
//正則表達(dá)式,驗(yàn)證第一位是否阿拉伯?dāng)?shù)字,確定轉(zhuǎn)換格式
//1.5億----混寫格式
if(Regex.IsMatch(strRMB,"^//d"))
{
//去掉元單位
strRMB = Regex.Replace(strRMB,"元|圓","");
char temp = strRMB[strRMB.Length - 1];
if (temp == '萬' || temp == '萬' || temp == '億')
{
return Convert.ToDouble(strRMB.Substring(0,strRMB.Length - 1)) * Math.Pow(10,GetExp(temp));
}
else
{
return Convert.ToDouble(strRMB);
}
}
//壹億伍千萬-----大寫格式
else
{
return Eval(strRMB);
}
}
catch
{
return -1;
}
}
///
/// 格式化(小寫轉(zhuǎn)大寫)
///
///
///
public static string Format(double numRMB)
{
try
{
if( 0 == numRMB)
return "零元整";
StringBuilder szRMB = new StringBuilder();
//乘100以格式成整型,便于處理
ulong iRMB = Convert.ToUInt64(numRMB * 100);
szRMB.Insert(0,ToUpper(Convert.ToInt32(iRMB % 100),-2));
//去掉原來的小數(shù)位
iRMB = iRMB / 100;
int iUnit = 0;
//以每4位為一個單位段進(jìn)行處理,所以下邊除以10000
while(iRMB != 0)
{
szRMB.Insert(0,ToUpper(Convert.ToInt32(iRMB % 10000),iUnit ));
iRMB = iRMB / 10000;
iUnit += 4;
}
string strRMB = szRMB.ToString();
//格式修正
strRMB = Regex.Replace(strRMB,"零+","零");
strRMB = strRMB.Replace("元零整","元整");
strRMB = strRMB.Replace("零元","元");
return strRMB.Trim('零');
}
catch
{
return "";
}
}
#endregion
#region 私有方法
///
/// 計(jì)算表達(dá)式(大寫表達(dá)式求值)
///
///
///
private static double Eval(string strRMB)
{
try
{
if (null == strRMB )
return 0;
strRMB = Replace(strRMB,false);
if ("" == strRMB)
return 0;
#region 利用位權(quán)進(jìn)行計(jì)算
//基礎(chǔ)指數(shù)
int basicExp = 0;
//當(dāng)前指數(shù)
int currExp = 0;
double numRMB = 0;
for(int i = strRMB.Length - 1; i > -1 ; i --)
{
char temp = strRMB[i];
if (temp == '元' || temp == '萬' || temp == '億' || temp == '圓' || temp == '萬' )
{
basicExp = GetExp(temp);
currExp = 0;
continue;
}
else
{
if(Regex.IsMatch(temp.ToString(),"^//d"))
{
numRMB = numRMB + Convert.ToInt32(temp.ToString()) * Math.Pow(10,(basicExp + currExp));
}
else
{
currExp = GetExp(temp);
}
}
}
#endregion
return numRMB;
}
catch
{
return -1;
}
}
///
/// 計(jì)算表達(dá)式(小寫數(shù)值求大寫字符串)
///
///
///
///
private static string ToUpper(int numRMB,int iUnit)
{
try
{
if( 0 == numRMB )
{
if (iUnit == -2)
{
return "整";
}
if (iUnit == 0)
{
return "元";
}
return "零";
}
StringBuilder szRMB = new StringBuilder();
string strRMB = "";
#region 對角/分做特殊處理
if (iUnit == -2)
{
int jiao = numRMB / 10;
int fen = numRMB % 10;
if (jiao > 0)
{
szRMB.Append(jiao);
szRMB.Append(GetUnit(-1));
if ( fen > 0)
{
szRMB.Append(fen);
szRMB.Append(GetUnit(-2));
}
}
else
{
szRMB.Append(fen);
szRMB.Append(GetUnit(-2));
}
return Replace(szRMB.ToString(),true);
}
#endregion
#region 以下為整數(shù)部分正常處理
strRMB = numRMB.ToString("0000");
//前一位是否是0
bool hasZero = false;
for ( int i = 0; i < strRMB.Length; i++ )
{
//只有四位,最高位為‘千’,所以下邊的3-i為單位修正
if ( ( 3-i ) > 0)
{
if( '0' != strRMB[i] )
{
szRMB.Append(strRMB[i]);
szRMB.Append(GetUnit( 3-i ));
hasZero = false;
}
else
{
if( !hasZero )
szRMB.Append(strRMB[i]);
hasZero = true;
}
}
//最后一位,特別格式處理
//如最后一位是零,則單位應(yīng)在零之前
else
{
if( '0' != strRMB[i] )
{
szRMB.Append(strRMB[i]);
szRMB.Append(GetUnit( iUnit ));
hasZero = false;
}
else
{
if (hasZero)
{
szRMB.Insert(szRMB.Length - 1,GetUnit( iUnit ));
}
else
{
szRMB.Append(GetUnit( iUnit ));
szRMB.Append(strRMB[i]);
}
}
}
}
//轉(zhuǎn)換大寫后返回
return Replace(szRMB.ToString(),true);
#endregion
}
catch
{
return "";
}
}
///
/// 將中文大寫換成阿拉伯?dāng)?shù)字
///
///
/// true--轉(zhuǎn)換為大寫/false--轉(zhuǎn)換為小寫
///
private static string Replace(string strRMB,bool toUpper)
{
if(toUpper)
{
strRMB = strRMB.Replace("0","零");
strRMB = strRMB.Replace("1","壹");
strRMB = strRMB.Replace("2","貳");
strRMB = strRMB.Replace("3","叁");
strRMB = strRMB.Replace("4","肆");
strRMB = strRMB.Replace("5","伍");
strRMB = strRMB.Replace("6","陸");
strRMB = strRMB.Replace("7","柒");
strRMB = strRMB.Replace("8","捌");
strRMB = strRMB.Replace("9","玖");
}
else
{
strRMB = strRMB.Replace("零","0");
strRMB = strRMB.Replace("壹","1");
strRMB = strRMB.Replace("貳","2");
strRMB = strRMB.Replace("叁","3");
strRMB = strRMB.Replace("肆","4");
strRMB = strRMB.Replace("伍","5");
strRMB = strRMB.Replace("陸","6");
strRMB = strRMB.Replace("柒","7");
strRMB = strRMB.Replace("捌","8");
strRMB = strRMB.Replace("玖","9");
}
return strRMB;
}
///
/// 獲取單位名稱
///
///
///
private static string GetUnit(int iCode)
{
switch(iCode)
{
case -2:
return "分";
case -1:
return "角";
case 0:
return "元";
case 1:
return "拾";
case 2:
return "佰";
case 3:
return "仟";
case 4:
return "萬";
case 8:
return "億";
default:
return "";
}
}
///
/// 獲取位權(quán)指數(shù)
///
///
///
private static int GetExp(char cUnit )
{
switch(cUnit)
{
case '分':
return -2;
case '角':
return -1;
case '元':
case '圓':
return 0;
case '十':
case '拾':
return 1;
case '百':
case '佰':
return 2;
case '千':
case '仟':
return 3;
case '萬':
case '萬':
return 4;
case '億':
return 8;
default:
return 0;
}
}
#endregion
}
}
本文由來源 21aspnet,由 system_mush 整理編輯,其版權(quán)均為 21aspnet 所有,文章內(nèi)容系作者個人觀點(diǎn),不代表 Java架構(gòu)師必看 對觀點(diǎn)贊同或支持。如需轉(zhuǎn)載,請注明文章來源。
總結(jié)
以上是生活随笔為你收集整理的java人民币大小写转换_人民币大小写转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 注意了!朋友圈发一张照片,是如何泄露你的
- 下一篇: IP SAN 实验(小白教程,超级具体)