MyException - 我的异常网
当前位置:我的异常网» C# » 数目字金额转换成大写汉字

数目字金额转换成大写汉字

www.MyException.Cn  网友分享于:2013-12-19  浏览:1112次
数字金额转换成大写汉字
比如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 "数值过大,无法转换! ";
}
}
}
}

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有