|
Use automata theory to do it. Divide the number into 4-digit sections (Section).
public class RMBConvert
{
public RMBConvert()
{
To
}
private static string[] faceVal = new String[] {"zero", "一", "贰", "三", "四",
"Wu", "Lu", "柒", "捌", "玖" };
private static string []unit = new string[]{"千","百","take",""};
private static string[] level = new String[] {"round", "take", "百", "1,000", "10,000", "100 million" };
public string Convert(string rmb)
{
int len = 0;
string ret = "";
string tmpY = "";
string tmpW = "";
string tmpG = "";
bool bFlag = false;
if(rmb.Length> 8)
rmb = rmb.PadLeft(12, '0');
else if(rmb.Length> 4)
rmb = rmb.PadLeft(8, '0');
else rmb = rmb.PadLeft(4, '0');
if(rmb.Length> 8)
{
tmpY = ConvertSection(rmb, 0, 4, false);
if(tmpY != "zero")
{
ret += tmpY + "billion";
bFlag = true;
}
tmpW = ConvertSection(rmb, 4, 4, bFlag);
if(tmpW != "zero")
{
ret += tmpW + "10,000";
bFlag = true;
}
tmpG = ConvertSection(rmb, 8, 4, bFlag);
if(tmpG != "zero")
ret += tmpG;
else{
if(tmpY == "zero"&&tmpW == "zero"&&tmpG == "zero")
ret = "zero";
}
}
else if(rmb.Length> 4)
{
tmpW = ConvertSection(rmb, 0, 4, false);
if(tmpW != "zero")
{
ret += tmpW + "10,000";
bFlag = true;
}
tmpG = ConvertSection(rmb, 4, 4, bFlag);
if(tmpG != "zero")
{
ret += tmpG;
}
else
{
if(tmpW == "zero")
ret = "zero";
}
}
else{
ret = ConvertSection(rmb, 0, 4, false);
}
return ret;
}
public string ConvertSection(string value, int start, int len, bool headerZero)
{
return ConvertSection(value.Substring(start, len), headerZero);
}
public string ConvertSection(string value)
{
return ConvertSection(value, false);
}
/// <summary>
/// Mainly look at this
/// </summary>
/// <param name="value"></param>
/// <param name="headerZero"></param>
/// <returns></returns>
public string ConvertSection(string value, bool headerZero)
{
string regVal = value.PadLeft(4, '0');
string strOutput = "";
int state = 0;
for(int i=0; i<4;i++)
{
int crr = regVal[i]-0x30;
switch(state)
{
case 0:
if( crr == 0)
{
state = 1;
}
else{
state = 2;
strOutput += faceVal[crr] + unit[i];
}
break;
case 1:
if( crr == 0)
{
state = 3;
}
else
{
state = 5;
if( headerZero)
strOutput += "zero";
strOutput += faceVal[crr] + unit[i];
}
break;
case 2:
if( crr == 0)
{
state = 4;
}
else
{
state = 5;
strOutput += faceVal[crr] + unit[i];
}
break;
case 3:
if( crr == 0)
{
state = 11;
}
else
{
state = 6;
if( headerZero)
strOutput += "zero";
strOutput += faceVal[crr] + unit[i];
}
break;
case 4:
if( crr == 0)
{
state = 7;
}
else
{
state = 6;
strOutput += "zero" + faceVal[crr] + unit[i];
}
break;
case 5:
if( crr == 0)
{
state = 7;
}
else
{
state = 6;
strOutput += faceVal[crr] + unit[i];
}
break;
case 6:
if( crr == 0)
{
state = 8;
}
else
{
state = 9;
strOutput += faceVal[crr] + unit[i];
}
break;
case 7:
if( crr == 0)
{
state = 10;
}
else
{
state = 9;
strOutput += "zero" + faceVal[crr] + unit[i];
}
break;
case 11:
if( crr == 0)
{
state = 12;
strOutput = "zero";
}
else
{
state = 13;
if( headerZero)
strOutput += "zero";
strOutput += faceVal[crr] + unit[i];
}
break;
case 8:break;
case 9:break;
case 10:break;
case 12:break;
case 13:break;
} // end of switch
} // end of for
return strOutput;
}
} |
|