封装的获得我国特色的农历、金额大写源代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace MyQuery.Utils
{
/// <summary>
/// 封装中国特色的处理
/// by 贾世义 2011-8-24
/// </summary>
public static class ChinaHelper
{
/// <summary>
/// 获得农历日期 农历正月廿三
/// </summary>
/// <param name="m_Date"></param>
/// <returns></returns>
public static string GetChineseDate(DateTime m_Date)
{
if (m_Date == null)
{
m_Date = DateTime.Today;
}
ChineseLunisolarCalendar cnCalendar = new ChineseLunisolarCalendar();
int cny = cnCalendar.GetSexagenaryYear(m_Date);
int cnm = cnCalendar.GetMonth(m_Date);
int cnd = cnCalendar.GetDayOfMonth(m_Date);
/*icnm = cnCalendar.GetLeapMonth(cnCalendar.GetYear(m_Date));
if (icnm > 0)
{
for (int i = icnm + 1; i < 13; i++)
cnMonth[icnm] = cnMonth[icnm - 1];
cnMonth[icnm] = "闰" + cnMonth[icnm];
}*/
string txcns = "";
const string szText1 = "癸甲乙丙丁戊己庚辛壬";
const string szText2 = "亥子丑寅卯辰巳午未申酉戌";
const string szText3 = "猪鼠牛虎免龙蛇马羊猴鸡狗";
int tn = cny % 10; //天干
int dn = cny % 12; //地支
txcns += szText1.Substring(tn, 1);
txcns += szText2.Substring(dn, 1);
txcns += "(" + szText3.Substring(dn, 1) + ")年";
//格式化月份显示
string[] cnMonth ={ "", "正月", "二月", "三月", "四月", "五月", "六月"
, "七月", "八月", "九月", "十月", "十一月", "十二月", "十二月" };
txcns += cnMonth[cnm];
string[] cnDay ={ "", "初一", "初二", "初三", "初四", "初五", "初六", "初七"
, "初八", "初九", "初十", "十一", "十二", "十三", "十四", "十五", "十六"
, "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五"
, "廿六", "廿七", "廿八", "廿九", "三十" };
txcns += cnDay[cnd];
return txcns;
}
/// <summary>
/// 获得公历日期 2008年08月08日
/// </summary>
/// <param name="m_Date"></param>
/// <returns></returns>
public static string GetDate(DateTime m_Date)
{
if (m_Date == null)
{
m_Date = DateTime.Today;
}
return m_Date.ToString("yyyy年MM月dd日");
}
///<summary>
/// 返回格式化的星期显示 星期日
///</summary>
///<param name="m_Date"></param>
///<returns></returns>
public static string GetWeek(DateTime m_Date)
{
if (m_Date == null)
{
m_Date = DateTime.Today;
}
int w = Convert.ToInt32(m_Date.DayOfWeek);
return "星期" + "日一二三四五六".Substring(w, 1);
}
/// <summary>
/// 获得人民币大写(带整)
/// </summary>
/// <param name="val">小写</param>
/// <returns></returns>
public static string GetRMB(double val)
{
if (val > 100000000000)
{
return val.ToString(); //不小于千亿
}
if (val == 0)
{
return "零元整";
}
string[] HanDigiStr = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
string[] HanDiviStr = { "","拾","佰","仟","万","拾","佰","仟","亿",
"拾","佰","仟","万","拾","佰","仟","亿",
"拾","佰","仟","万","拾","佰","仟" };
string SignStr = "", TailStr = "";
double fraction = 0;
Int64 integer = 0;
if (val < 0)
{
val = -val;
SignStr = "负";
}
//整数部分 由于ToInt64本身有四舍五入功能所以要加 val%1 部分
integer = Convert.ToInt64(val - val % 1);
int jiao, fen;
fraction = Math.Round(val % 1, 2); //四舍五入到分在.Net中四舍五入有点争议,它是伍不进位的。
fen = Convert.ToInt32((fraction * 100) % 10);//分
jiao = Convert.ToInt32((fraction * 100 - fen) / 10);//角
if (jiao == 0 && fen == 0)
{
TailStr = "整";
}
else
{
TailStr = HanDigiStr[jiao];
if (jiao != 0)
{
TailStr += "角";
}
if (integer == 0 && jiao == 0)
{
TailStr = ""; // 零元后不写零几分
}
if (fen != 0)
{
TailStr += HanDigiStr[fen] + "分";
}
}
string RMBStr = "";
if (integer == 0)
{
RMBStr = HanDigiStr[0];
}
else
{
string NumStr = integer.ToString();
int len, n;
bool hasvalue = false, lastzero = false; //亿、万进位前有数值标记
len = NumStr.Length;
for (int i = 0; i < len; i++)
{
int j = len - i - 1;
n = Convert.ToInt32(NumStr.Substring(i, 1));
if (n != 0)
{
if (lastzero)
{
RMBStr += HanDigiStr[0]; //若干零后若跟非零值,只显示一个零
}
//除了亿万前的零不带到后面
// if(!( n==1&&(i%4)==1 &&(lastzero || i==len-1))) //如十进位前有零也不发壹音用此行
// if( !( n==1 && (i%4)==1 && i==len-1 )) //十进位处于第一位不发壹音,
RMBStr += HanDigiStr[n];
RMBStr += HanDiviStr[len - i - 1]; //非零值后加进位,个位为空
hasvalue = true; //置万进位前有值标记
}
else
{
if (j == 8 || j == 4 && hasvalue) //亿万千百之间必须有非零值方显示万
{
RMBStr += HanDiviStr[len - i - 1]; //“亿”或“万”
}
}
if (j % 8 == 0)
{
hasvalue = false; // 万进位前有值标记逢亿复位
}
lastzero = (n == 0) && (j % 4 != 0); // 亿万前有零后不加零,如:拾万贰仟
}
}
return SignStr + RMBStr + "元" + TailStr;
}
}
}

你可能感兴趣的:(源代码)