您的位置:首页 > 编程语言 > C#

C#把数字转换为大写金额

2009-09-19 17:07 253 查看
private string ToBigNumber(long number)
{
// 12345 一万贰仟叁佰肆拾伍
string[] mm ={ '', '拾', '佰', '仟', '万', '拾', '佰',

'仟', '亿', '拾', '佰', '仟', '万' };
string[] dx = { '零', '壹', '贰', '叁', '肆', '伍',

'陆', '柒', '捌', '玖', '拾' };

if (number == 0)
{
return dx[0];
}
string numberStr = number.ToString();
if (numberStr.Length > mm.Length)
{
throw new UserException('can not parser number, as it it too long.');
}
StringBuilder buff = new StringBuilder();
int flag = 0;
int preNum = -1;
for (int i = numberStr.Length - 1; i >= 0; i--)
{
string currentBit = numberStr.Substring(flag++, 1);
if (int.Parse(currentBit) == 0)// deal with 0.
{
if (preNum != 0)// deal with 000
{
buff.Append(dx[int.Parse(currentBit)]);
}
}
else
{
buff.Append(dx[int.Parse(currentBit)]).Append(mm[i]);
}
preNum = int.Parse(currentBit);
}

//remove the latest zero.
string result = buff.ToString();
if (result.EndsWith(dx[0]))
{
buff.Remove(buff.Length - 1, 1);
}
return buff.ToString();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: