日期:2014-05-19  浏览次数:20918 次

数字金额转换成大写汉字
比如123456.88

转换成:壹拾贰万叁仟肆佰伍拾陆元捌角捌分


------解决方案--------------------
[ 2005-06-25 20:37:19 | 作者: yuhen ]
字号: 大 | 中 | 小
/* **********************************************
* Rainsoft Development Library for Microsoft.NET
*
* Copyright (c) 2004,2005 RainTrail Studio.China
* All Rigths Reserved!
* Author: Q.yuhen (qyuhen@hotmail.com)
********************************************** */
using System;
using System.Text;

namespace Rainsoft.Text
{
/// <summary>
/// 人民币大小写转换
/// </summary>
public sealed class RMB
{
private RMB() {}

/// <summary>
/// 人民币大小写转换。
/// </summary>
/// <param name= "r "> 货币金额 </param>
/// <returns> 人民币大写字符串 </returns>
/// <remarks>
/// 超过两位的小数会自动进行四舍五入。
/// </remarks>
public static string ToRmb(decimal r)
{
try
{
if (r == 0) return "零元 ";

string[] cnShuzi = { "零 ", "壹 ", "贰 ", "叁 ", "肆 ", "伍 ", "陆 ", "柒 ", "捌 ", "玖 " };
string[] cnDanwei = { "分 ", "角 ", "元 ", "拾 ", "佰 ", "仟 ", "万 ", "拾万 ", "佰万 ", "仟万 ",
"亿 ", "拾亿 ", "佰亿 ", "仟亿 ", "万亿 ", "拾万亿 ", "佰万亿 ", "仟万亿 ", "万万亿 " };

char[] arr = decimal.ToInt64(decimal.Round(r, 2) * 100).ToString().ToCharArray();
Array.Reverse(arr);

StringBuilder rmb = new StringBuilder();

for(int i = 0; i < arr.Length; i++)
{
int num = int.Parse(arr[i].ToString());

if (num != 0)
{
if (i > 6 && i < 10 && rmb.ToString().IndexOf( "万 ") > = 0)
rmb.Insert(0, cnShuzi[num] + cnDanwei[i][0]);
else if (i > 10 && i < 14 && rmb.ToString().IndexOf( "亿 ") > = 0)
rmb.Insert(0, cnShuzi[num] + cnDanwei[i][0]);
else if (i > 14 && rmb.ToString().IndexOf( "万亿 ") > = 0)
rmb.Insert(0, cnShuzi[num] + cnDanwei[i].Substring(0, 2));
else
rmb.Insert(0, cnShuzi[num] + cnDanwei[i]);
}
else
{
if (i == 2)
{
if (arr[1] == '0 ')
rmb.Insert(0, "元 ");
else
rmb.Insert(0, "元零 ");
}

if (rmb.Length > 0)
{
if (rmb[0] != '零 ' && rmb[0] != '元 ')
rmb.Insert(0, "零 ");
}
}
}

return rmb[rmb.Length - 1] == '元 ' ? rmb.ToString() + "整 " : rmb.ToString();
}
catch
{
return "数值过大,无法转换! ";
}
}
}
}