using System; namespace Numeric { #region 货币中文转换类 /// <summary> /// 本类实现阿拉伯数字到大写中文的转换 /// 该类没有对非法数字进行判别 /// 请调用NumToChn方法 /// 作者:menway /// </summary> public class NumericUpperConverter { #region 构造器 /// <summary> /// 构造器 /// </summary> public MoneyConverter() { // // TODO: Add constructor logic here // } #endregion #region 转换数字 /// <summary> /// 转换数字 /// </summary> /// <param name="x"></param> /// <returns></returns> private char ConvertNumeric(char p_chrValue) { string stringChnNames = "零一二三四五六七八九"; string stringNumNames = "0123456789"; return stringChnNames[stringNumNames.IndexOf(p_chrValue)]; } #endregion #region 转换万以下的整数 /// <summary> /// 转换万以下的整数 /// </summary> /// <param name="x"></param> /// <returns></returns> private string ConvertIntegerBelowThousand(string p_strValue) { string[] stringArrayLevelNames = new string[4] { "", "十", "百", "千" }; string strResult = string.Empty; int i1 = 0; for (i1 = p_strValue.Length - 1; i1 >= 0; i1--) { if (p_strValue[i1] == '0') { strResult = ConvertNumeric(p_strValue[i1]) + strResult; } else { strResult = ConvertNumeric(p_strValue[i1]) + stringArrayLevelNames[p_strValue.Length - 1 - i1] + strResult; } } while ((i1 = strResult.IndexOf("零零")) != -1) { strResult = strResult.Remove(i1, 1); } if (strResult[strResult.Length - 1] == '零' && strResult.Length > 1) { strResult = strResult.Remove(strResult.Length - 1, 1); } if (strResult.Length >= 2 && strResult.Substring(0, 2) == "一十") { strResult = strResult.Remove(0, 1); } return strResult; } #endregion #region 转换整数 /// <summary> /// 转换整数 /// </summary> /// <param name="x"></param> /// <returns></returns> private string ConvertInteger(string p_strValue) { int intLength = p_strValue.Length; string strResult = string.Empty; string strTemp = string.Empty; if (intLength <= 4) { strResult = ConvertIntegerBelowThousand(p_strValue); } else if (intLength <= 8) { strResult = ConvertIntegerBelowThousand(p_strValue.Substring(0, intLength - 4)) + "万"; strTemp = ConvertIntegerBelowThousand(p_strValue.Substring(intLength - 4, 4)); if (strTemp.IndexOf("千") == -1 && strTemp != string.Empty) { strResult += "零" + strTemp; } else { strResult += strTemp; } } else { strResult = ConvertIntegerBelowThousand(p_strValue.Substring(0, intLength - 8)) + "亿"; strTemp = ConvertIntegerBelowThousand(p_strValue.Substring(intLength - 8, 4)); if (strTemp.IndexOf("千") == -1 && strTemp != "") { strResult += "零" + strTemp; } else { strResult += strTemp; } strResult += "万"; strTemp = ConvertIntegerBelowThousand(p_strValue.Substring(intLength - 4, 4)); if (strTemp.IndexOf("千") == -1 && strTemp != "") { strResult += "零" + strTemp; } else { strResult += strTemp; } } int i1 = 0; if ((i1 = strResult.IndexOf("零万")) != -1) { strResult = strResult.Remove(i1 + 1, 1); } while ((i1 = strResult.IndexOf("零零")) != -1) { strResult = strResult.Remove(i1, 1); } if (strResult[strResult.Length - 1] == '零' && strResult.Length > 1) { strResult = strResult.Remove(strResult.Length - 1, 1); } return strResult; } #endregion #region 转换小数 /// <summary> /// 转换小数 /// </summary> /// <param name="x"></param> /// <returns></returns> private string ConvertDecimal(string p_strValue) { string strResult = string.Empty; for (int i = 0; i < p_strValue.Length; i++) { strResult += ConvertNumeric(p_strValue[i]); } return strResult; } #endregion #region 数字转换成中文 /// <summary> /// 数字转换成中文 /// </summary> /// <param name="x"></param> /// <returns></returns> public static string ConvertToChinese(string p_strValue) { string strResult = string.Empty; if (p_strValue.Length == 0) { return strResult; } if (p_strValue[0] == '-') { strResult = "负"; p_strValue = p_strValue.Remove(0, 1); } if (p_strValue[0].ToString() == ".") { p_strValue = "0" + p_strValue; } if (p_strValue[p_strValue.Length - 1].ToString() == ".") { p_strValue = p_strValue.Remove(p_strValue.Length - 1, 1); } MoneyConverter objConverter = new MoneyConverter(); if (p_strValue.IndexOf(".") > -1) { strResult += objConverter.ConvertInteger(p_strValue.Substring(0, p_strValue.IndexOf("."))) + "点" + objConverter.ConvertDecimal(p_strValue.Substring(p_strValue.IndexOf(".") + 1)); } else { strResult += objConverter.ConvertInteger(p_strValue); } objConverter = null; return strResult; } #endregion } #endregion }
浙公网安备 33010602011771号