问题原文:【C# 每日一题4】编程实现题 [推荐]
周末加班弄了弄,周1晚上又测试了一下.没发现什么大问题.现在发布完整版本.由于代码太多,可能无法一次发上来.有兴趣的可以去我的CSDN博客去看.
一个数字转中文大写货币数字的类.完美兼容于C#所有值类型转换和操作,beta版.希望大家多多纠正.下面是代码.考虑到很多问题,所以写的有点乱,大家别看花眼就好.
public struct CHMoney : IComparable, IFormattable, IConvertible, IComparable<CHMoney>, IEquatable<CHMoney>
{
#region 常量
/// <summary>
/// 大写数字 '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'
/// </summary>
private static readonly char[] CHU = new char[] { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };
/// <summary>
/// 汉字数字 '〇', '一', '二', '三', '四', '五', '六', '七', '八', '九'
/// </summary>
private static readonly char[] CHL = new char[] { '〇', '一', '二', '三', '四', '五', '六', '七', '八', '九' };
/// <summary>
/// 大写单位 0-整, 1-分, 2-角, 3-圆, 4-拾, 5-佰, 6-仟, 7-萬, 8-億
/// </summary>
private static readonly char[] CHUUNIT = new char[] { '整', '分', '角', '圆', '拾', '佰', '仟', '萬', '億' };
/// <summary>
/// 数字单位 0-整, 1-分, 2-角, 3-元, 4-十, 5-百, 6-千, 7-万, 8-亿
/// </summary>
private static readonly char[] CHLUNIT = new char[] { '整', '分', '角', '元', '十', '百', '千', '万', '亿' };
/// <summary>
/// 符号 0-负, 1-正
/// </summary>
private static readonly char[] SIGNED = new char[] { '负', '正' };
/// <summary>
/// 假真
/// </summary>
private static readonly string[] BOOLEAN = new string[] { "假", "真" };
/// <summary>
/// 类型定义 0-bool,1-sbyte, 2-byte, 3-short, 4-char, 5-ushort, 6-int, 7-uint, 8-long, 9-ulong, 10-decimal, 11-float, 12-double
/// </summary>
private static readonly Type[] TYPE = new Type[] {
typeof(bool), typeof(sbyte),
typeof(byte), typeof(short),
typeof(char), typeof(ushort),
typeof(int), typeof(uint),
typeof(long), typeof(ulong),
typeof(decimal), typeof(float),
typeof(double)
}; #endregion 常量
#region 私有变量
/// <summary>
/// 数据源
/// </summary>
private object _Source;
/// <summary>
/// 大写
/// </summary>
private string _Upper;
/// <summary>
/// 小写
/// </summary>
private string _Lower; #endregion 私有变量 #region 成员属性
/// <summary>
/// 获取原始数据类型
/// </summary>
public Type SourceType
{
get
{
if (this._Source == null)
{
Init();
}
return this._Source.GetType();
}
}
#endregion 成员属性 #region 公共方法
/// <summary>
/// 转换为大写数字形式
/// </summary>
/// <returns></returns>
public string ToUpper()
{
if (_Upper == null)
{
Init();
}
return _Upper;
}
/// <summary>
/// 转换为小写数字形式
/// </summary>
/// <returns></returns>
public string ToLower()
{
if (_Lower == null)
{
Init();
}
return _Lower;
}
#endregion 公共方法 //#region 静态方法
/// <summary>
/// 创建一个CHMoney对象
/// </summary>
/// <param name="chmoneyString">中国钱币数字字符串</param>
/// <returns></returns>
public static CHMoney CreateNewFromCHMoneyString(string chmoneyString)
{
return new CHMoney();//这个还没写
}
//#endregion 静态方法 #region 私有方法
private void Init()
{
this._Source = new Int32();
this._Upper = "零";
this._Lower = "〇";
}
#endregion 私有方法 #region 构造函数 /// <summary>
/// 从字符串创建一个CHMoney
/// </summary>
/// <param name="src">具有十进制数字的字符串,允许有小数点和符号</param>
/// <exception cref="ArgumentException">给定的字符串无法解析时引发此异常</exception>
/// <exception cref="ArgumentNullException">给定的字符串为空时引发此异常</exception>
public CHMoney(string src)
{
if (src == null || src == "")
{
throw new ArgumentNullException("Can not create an object from the empty string");
} //bool signed = src[0] == '-'; //本来可以写细节的,不过由于时间问题,懒得写了,
//下次公布所有.net内部所有的代码给大家,大家就知道内部是怎么实现转换的了. bool tbool;
int tint;
long tlong;
decimal tdecimal;
double tdouble; if (bool.TryParse(src, out tbool))
{
this = new CHMoney(tbool);
}
else if (int.TryParse(src, out tint))
{
this = new CHMoney(tint);
}
else if (long.TryParse(src, out tlong))
{
this = new CHMoney(tlong);
}
else if (decimal.TryParse(src, out tdecimal))
{
this = new CHMoney(tdecimal);
}
else if (double.TryParse(src, out tdouble))
{
this = new CHMoney(tdouble);
}
else
{
throw new ArgumentException("Can not resolve the given string");
}
}
/// <summary>
/// 从值类型对象创建一个CHMoney
/// </summary>
/// <param name="src">值类型对象</param>
/// <exception cref="ArgumentException">给定的字符串无法解析时引发此异常</exception>
/// <exception cref="ArgumentNullException">给定的字符串为空时引发此异常</exception>
public CHMoney(object src)
{
if (src != null)
{
Type t = src.GetType(); int level = -1; for (int i = 0; i < TYPE.Length; i++)
{
if (t == TYPE[i])
{
level = i;
}
}
if (t == typeof(string))
{
level = 13;
}
if (level == -1)
{
throw new ArgumentException("The type is not supported");
}
else
{
this = new CHMoney(src, level);
} }
else
{
throw new ArgumentNullException("Can not create an object from the empty string");
}
}
周末加班弄了弄,周1晚上又测试了一下.没发现什么大问题.现在发布完整版本.由于代码太多,可能无法一次发上来.有兴趣的可以去我的CSDN博客去看.
一个数字转中文大写货币数字的类.完美兼容于C#所有值类型转换和操作,beta版.希望大家多多纠正.下面是代码.考虑到很多问题,所以写的有点乱,大家别看花眼就好.
public struct CHMoney : IComparable, IFormattable, IConvertible, IComparable<CHMoney>, IEquatable<CHMoney>
{
#region 常量
/// <summary>
/// 大写数字 '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'
/// </summary>
private static readonly char[] CHU = new char[] { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };
/// <summary>
/// 汉字数字 '〇', '一', '二', '三', '四', '五', '六', '七', '八', '九'
/// </summary>
private static readonly char[] CHL = new char[] { '〇', '一', '二', '三', '四', '五', '六', '七', '八', '九' };
/// <summary>
/// 大写单位 0-整, 1-分, 2-角, 3-圆, 4-拾, 5-佰, 6-仟, 7-萬, 8-億
/// </summary>
private static readonly char[] CHUUNIT = new char[] { '整', '分', '角', '圆', '拾', '佰', '仟', '萬', '億' };
/// <summary>
/// 数字单位 0-整, 1-分, 2-角, 3-元, 4-十, 5-百, 6-千, 7-万, 8-亿
/// </summary>
private static readonly char[] CHLUNIT = new char[] { '整', '分', '角', '元', '十', '百', '千', '万', '亿' };
/// <summary>
/// 符号 0-负, 1-正
/// </summary>
private static readonly char[] SIGNED = new char[] { '负', '正' };
/// <summary>
/// 假真
/// </summary>
private static readonly string[] BOOLEAN = new string[] { "假", "真" };
/// <summary>
/// 类型定义 0-bool,1-sbyte, 2-byte, 3-short, 4-char, 5-ushort, 6-int, 7-uint, 8-long, 9-ulong, 10-decimal, 11-float, 12-double
/// </summary>
private static readonly Type[] TYPE = new Type[] {
typeof(bool), typeof(sbyte),
typeof(byte), typeof(short),
typeof(char), typeof(ushort),
typeof(int), typeof(uint),
typeof(long), typeof(ulong),
typeof(decimal), typeof(float),
typeof(double)
}; #endregion 常量
#region 私有变量
/// <summary>
/// 数据源
/// </summary>
private object _Source;
/// <summary>
/// 大写
/// </summary>
private string _Upper;
/// <summary>
/// 小写
/// </summary>
private string _Lower; #endregion 私有变量 #region 成员属性
/// <summary>
/// 获取原始数据类型
/// </summary>
public Type SourceType
{
get
{
if (this._Source == null)
{
Init();
}
return this._Source.GetType();
}
}
#endregion 成员属性 #region 公共方法
/// <summary>
/// 转换为大写数字形式
/// </summary>
/// <returns></returns>
public string ToUpper()
{
if (_Upper == null)
{
Init();
}
return _Upper;
}
/// <summary>
/// 转换为小写数字形式
/// </summary>
/// <returns></returns>
public string ToLower()
{
if (_Lower == null)
{
Init();
}
return _Lower;
}
#endregion 公共方法 //#region 静态方法
/// <summary>
/// 创建一个CHMoney对象
/// </summary>
/// <param name="chmoneyString">中国钱币数字字符串</param>
/// <returns></returns>
public static CHMoney CreateNewFromCHMoneyString(string chmoneyString)
{
return new CHMoney();//这个还没写
}
//#endregion 静态方法 #region 私有方法
private void Init()
{
this._Source = new Int32();
this._Upper = "零";
this._Lower = "〇";
}
#endregion 私有方法 #region 构造函数 /// <summary>
/// 从字符串创建一个CHMoney
/// </summary>
/// <param name="src">具有十进制数字的字符串,允许有小数点和符号</param>
/// <exception cref="ArgumentException">给定的字符串无法解析时引发此异常</exception>
/// <exception cref="ArgumentNullException">给定的字符串为空时引发此异常</exception>
public CHMoney(string src)
{
if (src == null || src == "")
{
throw new ArgumentNullException("Can not create an object from the empty string");
} //bool signed = src[0] == '-'; //本来可以写细节的,不过由于时间问题,懒得写了,
//下次公布所有.net内部所有的代码给大家,大家就知道内部是怎么实现转换的了. bool tbool;
int tint;
long tlong;
decimal tdecimal;
double tdouble; if (bool.TryParse(src, out tbool))
{
this = new CHMoney(tbool);
}
else if (int.TryParse(src, out tint))
{
this = new CHMoney(tint);
}
else if (long.TryParse(src, out tlong))
{
this = new CHMoney(tlong);
}
else if (decimal.TryParse(src, out tdecimal))
{
this = new CHMoney(tdecimal);
}
else if (double.TryParse(src, out tdouble))
{
this = new CHMoney(tdouble);
}
else
{
throw new ArgumentException("Can not resolve the given string");
}
}
/// <summary>
/// 从值类型对象创建一个CHMoney
/// </summary>
/// <param name="src">值类型对象</param>
/// <exception cref="ArgumentException">给定的字符串无法解析时引发此异常</exception>
/// <exception cref="ArgumentNullException">给定的字符串为空时引发此异常</exception>
public CHMoney(object src)
{
if (src != null)
{
Type t = src.GetType(); int level = -1; for (int i = 0; i < TYPE.Length; i++)
{
if (t == TYPE[i])
{
level = i;
}
}
if (t == typeof(string))
{
level = 13;
}
if (level == -1)
{
throw new ArgumentException("The type is not supported");
}
else
{
this = new CHMoney(src, level);
} }
else
{
throw new ArgumentNullException("Can not create an object from the empty string");
}
}
解决方案 »
- ckeditor3.5.3 图片上传问题
- C#如何获取QQ安装路径 ?并且让其获取textbox的值进行登陆?
- C#创建应用程序出现错误!!
- 如何设置应用程序访问文件的时间间隔???
- 求助16进制的问题
- 如何给第三方组件加强名称.我安装Framework SDK后怎么找不到tblimp.exe,请问在哪?
- 我建了一个qq群,欢迎志同道合者加盟!
- 数据库应该如何建立呢
- 百分求解,如何让textarea在没有文字输入,或者文字没有换行之前不显示滚动条,向windows里的文本框一样。
- 在线请教 ,怎么把一个时间2003-09-07 转化成数字 0309 ?越简单越好,俺现在在公司加班
- 寻一个简单的成绩统计的算法
- 请教:同事留下的项目引用的两个ActiveX是什么?
private CHMoney(object src, int level)
{ this._Source = src; Type t = src.GetType(); string str = src.ToString(); if (level == 0)
{
this._Source = src;
this._Upper = BOOLEAN[bool.TrueString.Equals(str) ? 1 : 0];
this._Lower = _Upper;
}
else if (level < 10)
{
//(StringBuilder.Append), (string + string) 这些都是扯淡,最浪费cpu时间的就是这个了.
//最后选择了char数组,虽然代码编写上有难度,但是效率提高了很多,
//CHUUNIT和CHLUNIT可以定义成按位的字符串数组,虽然编写代码容易了,但是字符串的操作浪费了效率. #region 转换细节
char[] chl, chu; if (str[0] == '-')
{
chl = new char[str.Length * 2 + 2];
chu = new char[chl.Length];
}
else
{
chl = new char[str.Length * 2 + 1];
chu = new char[chl.Length];
} chu[chu.Length - 1] = CHUUNIT[0];//'整'
chl[chu.Length - 1] = CHLUNIT[0];//'整'
chu[chu.Length - 2] = CHUUNIT[3];//'元'
chl[chu.Length - 2] = CHLUNIT[3];//'元' int len = str.Length, //字符串长度
quo = len >> 2, //字符串长度除以4
rem = len - (quo << 2), //字符串长度除以4剩下的余数
chi = chl.Length - 1, //缓冲区起始索引
sti = chi >> 1, //字符串起始索引
off = 2, //偏移量
enu = 0; //枚举值 for (int i = 1; i <= quo; i++, chi -= 8, sti -= 4, enu = 0)
{
if (str[sti - 1] == '0') { enu += 1; }
if (str[sti - 2] == '0') { enu += 2; }
if (str[sti - 3] == '0') { enu += 4; }
if (str[sti - 4] == '0') { enu += 8; } if (enu == 0xF && (i >> 1 << 1) == i)
{
off--;
}
switch (enu)
{
case 0://0000
{
chl[chi - off/**/] = CHL[str[sti - 1] - 48];
chl[chi - off - 1] = CHLUNIT[4];
chl[chi - off - 2] = CHL[str[sti - 2] - 48];
chl[chi - off - 3] = CHLUNIT[5];
chl[chi - off - 4] = CHL[str[sti - 3] - 48];
chl[chi - off - 5] = CHLUNIT[6];
chl[chi - off - 6] = CHL[str[sti - 4] - 48]; chu[chi - off/**/] = CHU[str[sti - 1] - 48];
chu[chi - off - 1] = CHUUNIT[4];
chu[chi - off - 2] = CHU[str[sti - 2] - 48];
chu[chi - off - 3] = CHUUNIT[5];
chu[chi - off - 4] = CHU[str[sti - 3] - 48];
chu[chi - off - 5] = CHUUNIT[6];
chu[chi - off - 6] = CHU[str[sti - 4] - 48]; if (i == quo && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 7] = CHLUNIT[8];
chu[chi - off - 7] = CHUUNIT[8];
}
else
{
chl[chi - off - 7] = CHLUNIT[7];
chu[chi - off - 7] = CHUUNIT[7];
}
}
break;
case 1://0001
{
//chl[chi - off/**/] = CHL[str[sti - 1] - 48];
chl[chi - off/**/] = CHLUNIT[4];
chl[chi - off - 1] = CHL[str[sti - 2] - 48];
chl[chi - off - 2] = CHLUNIT[5];
chl[chi - off - 3] = CHL[str[sti - 3] - 48];
chl[chi - off - 4] = CHLUNIT[6];
chl[chi - off - 5] = CHL[str[sti - 4] - 48]; //chu[chi - off/**/] = CHU[str[sti - 1] - 48];
chu[chi - off/**/] = CHUUNIT[4];
chu[chi - off - 1] = CHU[str[sti - 2] - 48];
chu[chi - off - 2] = CHUUNIT[5];
chu[chi - off - 3] = CHU[str[sti - 3] - 48];
chu[chi - off - 4] = CHUUNIT[6];
chu[chi - off - 5] = CHU[str[sti - 4] - 48]; if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 6] = CHLUNIT[8];
chu[chi - off - 6] = CHUUNIT[8];
}
else
{
chl[chi - off - 6] = CHLUNIT[7];
chu[chi - off - 6] = CHUUNIT[7];
}
off--;
}
break;
case 2://0010
{
chl[chi - off/**/] = CHL[str[sti - 1] - 48];
//chl[chi - off - 1] = CHLUNIT[4];
chl[chi - off - 1] = CHL[str[sti - 2] - 48];
chl[chi - off - 2] = CHLUNIT[5];
chl[chi - off - 3] = CHL[str[sti - 3] - 48];
chl[chi - off - 4] = CHLUNIT[6];
chl[chi - off - 5] = CHL[str[sti - 4] - 48]; chu[chi - off/**/] = CHU[str[sti - 1] - 48];
//chu[chi - off - 1] = CHUUNIT[4];
chu[chi - off - 1] = CHU[str[sti - 2] - 48];
chu[chi - off - 2] = CHUUNIT[5];
chu[chi - off - 3] = CHU[str[sti - 3] - 48];
chu[chi - off - 4] = CHUUNIT[6];
chu[chi - off - 5] = CHU[str[sti - 4] - 48]; if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 6] = CHLUNIT[8];
chu[chi - off - 6] = CHUUNIT[8];
}
else
{
chl[chi - off - 6] = CHLUNIT[7];
chu[chi - off - 6] = CHUUNIT[7];
}
off--;
}
break;
case 3://0011
{
//chl[chi - off/**/] = CHL[str[sti - 1] - 48];
//chl[chi - off - 1] = CHLUNIT[4];
chl[chi - off/**/] = CHL[str[sti - 2] - 48];
chl[chi - off - 1] = CHLUNIT[5];
chl[chi - off - 2] = CHL[str[sti - 3] - 48];
chl[chi - off - 3] = CHLUNIT[6];
chl[chi - off - 4] = CHL[str[sti - 4] - 48]; //chu[chi - off/**/] = CHU[str[sti - 1] - 48];
//chu[chi - off - 1] = CHUUNIT[4];
chu[chi - off/**/] = CHU[str[sti - 2] - 48];
chu[chi - off - 1] = CHUUNIT[5];
chu[chi - off - 2] = CHU[str[sti - 3] - 48];
chu[chi - off - 3] = CHUUNIT[6];
chu[chi - off - 4] = CHU[str[sti - 4] - 48]; if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 5] = CHLUNIT[8];
chu[chi - off - 5] = CHUUNIT[8];
}
else
{
chl[chi - off - 5] = CHLUNIT[7];
chu[chi - off - 5] = CHUUNIT[7];
}
off -= 2;
}
break;
case 4://0100
{
chl[chi - off/**/] = CHL[str[sti - 1] - 48];
chl[chi - off - 1] = CHLUNIT[4];
chl[chi - off - 2] = CHL[str[sti - 2] - 48];
//chl[chi - off - 3] = CHLUNIT[5];
chl[chi - off - 3] = CHL[str[sti - 3] - 48];
chl[chi - off - 4] = CHLUNIT[6];
chl[chi - off - 5] = CHL[str[sti - 4] - 48]; chu[chi - off/**/] = CHU[str[sti - 1] - 48];
chu[chi - off - 1] = CHUUNIT[4];
chu[chi - off - 2] = CHU[str[sti - 2] - 48];
//chu[chi - off - 3] = CHUUNIT[5];
chu[chi - off - 3] = CHU[str[sti - 3] - 48];
chu[chi - off - 4] = CHUUNIT[6];
chu[chi - off - 5] = CHU[str[sti - 4] - 48]; if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 6] = CHLUNIT[8];
chu[chi - off - 6] = CHUUNIT[8];
}
else
{
chl[chi - off - 6] = CHLUNIT[7];
chu[chi - off - 6] = CHUUNIT[7];
}
off--;
}
break;
case 5://0101
{
//chl[chi - off/**/] = CHL[str[sti - 1] - 48];
chl[chi - off/**/] = CHLUNIT[4];
chl[chi - off - 1] = CHL[str[sti - 2] - 48];
//chl[chi - off - 3] = CHLUNIT[5];
chl[chi - off - 2] = CHL[str[sti - 3] - 48];
chl[chi - off - 3] = CHLUNIT[6];
chl[chi - off - 4] = CHL[str[sti - 4] - 48]; //chu[chi - off/**/] = CHU[str[sti - 1] - 48];
chu[chi - off/**/] = CHUUNIT[4];
chu[chi - off - 1] = CHU[str[sti - 2] - 48];
//chu[chi - off - 3] = CHUUNIT[5];
chu[chi - off - 2] = CHU[str[sti - 3] - 48];
chu[chi - off - 3] = CHUUNIT[6];
chu[chi - off - 4] = CHU[str[sti - 4] - 48]; if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 5] = CHLUNIT[8];
chu[chi - off - 5] = CHUUNIT[8];
}
else
{
chl[chi - off - 5] = CHLUNIT[7];
chu[chi - off - 5] = CHUUNIT[7];
}
off -= 2;
}
break;
case 6://0110
{
chl[chi - off/**/] = CHL[str[sti - 1] - 48];
//chl[chi - off - 1] = CHLUNIT[4];
//chl[chi - off - 2] = CHL[str[sti - 2] - 48];
//chl[chi - off - 3] = CHLUNIT[5];
chl[chi - off - 1] = CHL[str[sti - 3] - 48];
chl[chi - off - 2] = CHLUNIT[6];
chl[chi - off - 3] = CHL[str[sti - 4] - 48]; chu[chi - off/**/] = CHU[str[sti - 1] - 48];
//chu[chi - off - 1] = CHUUNIT[4];
//chu[chi - off - 2] = CHU[str[sti - 2] - 48];
//chu[chi - off - 3] = CHUUNIT[5];
chu[chi - off - 1] = CHU[str[sti - 3] - 48];
chu[chi - off - 2] = CHUUNIT[6];
chu[chi - off - 3] = CHU[str[sti - 4] - 48]; if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 4] = CHLUNIT[8];
chu[chi - off - 4] = CHUUNIT[8];
}
else
{
chl[chi - off - 4] = CHLUNIT[7];
chu[chi - off - 4] = CHUUNIT[7];
}
off -= 3;
}
break;
case 7://0111
{
//chl[chi - off/**/] = CHL[str[sti - 1] - 48];
//chl[chi - off - 1] = CHLUNIT[4];
//chl[chi - off - 2] = CHL[str[sti - 2] - 48];
//chl[chi - off - 3] = CHLUNIT[5];
//chl[chi - off - 4] = CHL[str[sti - 3] - 48];
chl[chi - off/**/] = CHLUNIT[6];
chl[chi - off - 1] = CHL[str[sti - 4] - 48]; //chu[chi - off/**/] = CHU[str[sti - 1] - 48];
//chu[chi - off - 1] = CHUUNIT[4];
//chu[chi - off - 2] = CHU[str[sti - 2] - 48];
//chu[chi - off - 3] = CHUUNIT[5];
//chu[chi - off - 4] = CHU[str[sti - 3] - 48];
chu[chi - off/**/] = CHUUNIT[6];
chu[chi - off - 1] = CHU[str[sti - 4] - 48]; if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 2] = CHLUNIT[8];
chu[chi - off - 2] = CHUUNIT[8];
}
else
{
chl[chi - off - 2] = CHLUNIT[7];
chu[chi - off - 2] = CHUUNIT[7];
}
off -= 5;
}
break;
case 8://1000
{
chl[chi - off/**/] = CHL[str[sti - 1] - 48];
chl[chi - off - 1] = CHLUNIT[4];
chl[chi - off - 2] = CHL[str[sti - 2] - 48];
chl[chi - off - 3] = CHLUNIT[5];
chl[chi - off - 4] = CHL[str[sti - 3] - 48];
//chl[chi - off - 5] = CHLUNIT[6];
chl[chi - off - 5] = CHL[str[sti - 4] - 48]; chu[chi - off/**/] = CHU[str[sti - 1] - 48];
chu[chi - off - 1] = CHUUNIT[4];
chu[chi - off - 2] = CHU[str[sti - 2] - 48];
chu[chi - off - 3] = CHUUNIT[5];
chu[chi - off - 4] = CHU[str[sti - 3] - 48];
//chu[chi - off - 5] = CHUUNIT[6];
chu[chi - off - 5] = CHU[str[sti - 4] - 48]; if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 6] = CHLUNIT[8];
chu[chi - off - 6] = CHUUNIT[8];
}
else
{
chl[chi - off - 6] = CHLUNIT[7];
chu[chi - off - 6] = CHUUNIT[7];
}
off--;
}
break;
case 9://1001
{
//chl[chi - off/**/] = CHL[str[sti - 1] - 48];
chl[chi - off/**/] = CHLUNIT[4];
chl[chi - off - 1] = CHL[str[sti - 2] - 48];
chl[chi - off - 2] = CHLUNIT[5];
chl[chi - off - 3] = CHL[str[sti - 3] - 48];
//chl[chi - off - 5] = CHLUNIT[6];
chl[chi - off - 4] = CHL[str[sti - 4] - 48]; //chu[chi - off/**/] = CHU[str[sti - 1] - 48];
chu[chi - off/**/] = CHUUNIT[4];
chu[chi - off - 1] = CHU[str[sti - 2] - 48];
chu[chi - off - 2] = CHUUNIT[5];
chu[chi - off - 3] = CHU[str[sti - 3] - 48];
//chu[chi - off - 5] = CHUUNIT[6];
chu[chi - off - 4] = CHU[str[sti - 4] - 48]; if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 5] = CHLUNIT[8];
chu[chi - off - 5] = CHUUNIT[8];
}
else
{
chl[chi - off - 5] = CHLUNIT[7];
chu[chi - off - 5] = CHUUNIT[7];
}
off -= 2;
}
break;
case 10://1010
{
//chl[chi - off/**/] = CHL[str[sti - 1] - 48];
chl[chi - off/**/] = CHLUNIT[4];
chl[chi - off - 1] = CHL[str[sti - 2] - 48];
//chl[chi - off - 3] = CHLUNIT[5];
chl[chi - off - 2] = CHL[str[sti - 3] - 48];
chl[chi - off - 3] = CHLUNIT[6];
chl[chi - off - 4] = CHL[str[sti - 4] - 48]; //chu[chi - off/**/] = CHU[str[sti - 1] - 48];
chu[chi - off/**/] = CHUUNIT[4];
chu[chi - off - 1] = CHU[str[sti - 2] - 48];
//chu[chi - off - 3] = CHUUNIT[5];
chu[chi - off - 2] = CHU[str[sti - 3] - 48];
chu[chi - off - 3] = CHUUNIT[6];
chu[chi - off - 4] = CHU[str[sti - 4] - 48]; if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 5] = CHLUNIT[8];
chu[chi - off - 5] = CHUUNIT[8];
}
else
{
chl[chi - off - 5] = CHLUNIT[7];
chu[chi - off - 5] = CHUUNIT[7];
}
off -= 2;
}
break;
case 11://1011
{
//chl[chi - off/**/] = CHL[str[sti - 1] - 48];
//chl[chi - off - 1] = CHLUNIT[4];
//chl[chi - off - 2] = CHL[str[sti - 2] - 48];
chl[chi - off/**/] = CHLUNIT[5];
chl[chi - off - 1] = CHL[str[sti - 3] - 48];
//chl[chi - off - 5] = CHLUNIT[6];
chl[chi - off - 2] = CHL[str[sti - 4] - 48]; //chu[chi - off/**/] = CHU[str[sti - 1] - 48];
//chu[chi - off - 1] = CHUUNIT[4];
//chu[chi - off - 2] = CHU[str[sti - 2] - 48];
chu[chi - off/**/] = CHUUNIT[5];
chu[chi - off - 1] = CHU[str[sti - 3] - 48];
//chu[chi - off - 5] = CHUUNIT[6];
chu[chi - off - 2] = CHU[str[sti - 4] - 48]; if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 3] = CHLUNIT[8];
chu[chi - off - 3] = CHUUNIT[8];
}
else
{
chl[chi - off - 3] = CHLUNIT[7];
chu[chi - off - 3] = CHUUNIT[7];
}
off -= 4;
}
break;
case 12://1100
{
chl[chi - off/**/] = CHL[str[sti - 1] - 48];
chl[chi - off - 1] = CHLUNIT[4];
chl[chi - off - 2] = CHL[str[sti - 2] - 48];
//chl[chi - off - 3] = CHLUNIT[5];
//chl[chi - off - 4] = CHL[str[sti - 3] - 48];
//chl[chi - off - 5] = CHLUNIT[6];
chl[chi - off - 3] = CHL[str[sti - 4] - 48]; chu[chi - off/**/] = CHU[str[sti - 1] - 48];
chu[chi - off - 1] = CHUUNIT[4];
chu[chi - off - 2] = CHU[str[sti - 2] - 48];
//chu[chi - off - 3] = CHUUNIT[5];
//chu[chi - off - 4] = CHU[str[sti - 3] - 48];
//chu[chi - off - 5] = CHUUNIT[6];
chu[chi - off - 3] = CHU[str[sti - 4] - 48]; if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 4] = CHLUNIT[8];
chu[chi - off - 4] = CHUUNIT[8];
}
else
{
chl[chi - off - 4] = CHLUNIT[7];
chu[chi - off - 4] = CHUUNIT[7];
}
off -= 3;
}
break;
case 13://1101
{
//chl[chi - off/**/] = CHL[str[sti - 1] - 48];
chl[chi - off/**/] = CHLUNIT[4];
chl[chi - off - 1] = CHL[str[sti - 2] - 48];
//chl[chi - off - 3] = CHLUNIT[5];
//chl[chi - off - 4] = CHL[str[sti - 3] - 48];
//chl[chi - off - 5] = CHLUNIT[6];
chl[chi - off - 2] = CHL[str[sti - 4] - 48]; //chu[chi - off/**/] = CHU[str[sti - 1] - 48];
chu[chi - off/**/] = CHUUNIT[4];
chu[chi - off - 1] = CHU[str[sti - 2] - 48];
//chu[chi - off - 3] = CHUUNIT[5];
//chu[chi - off - 4] = CHU[str[sti - 3] - 48];
//chu[chi - off - 5] = CHUUNIT[6];
chu[chi - off - 2] = CHU[str[sti - 4] - 48]; if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 3] = CHLUNIT[8];
chu[chi - off - 3] = CHUUNIT[8];
}
else
{
chl[chi - off - 3] = CHLUNIT[7];
chu[chi - off - 3] = CHUUNIT[7];
}
off -= 4;
}
break;
case 14://1110
{
chl[chi - off/**/] = CHL[str[sti - 1] - 48];
//chl[chi - off - 1] = CHLUNIT[4];
//chl[chi - off - 2] = CHL[str[sti - 2] - 48];
//chl[chi - off - 3] = CHLUNIT[5];
//chl[chi - off - 4] = CHL[str[sti - 3] - 48];
//chl[chi - off - 5] = CHLUNIT[6];
chl[chi - off - 1] = CHL[str[sti - 4] - 48]; chu[chi - off/**/] = CHU[str[sti - 1] - 48];
//chu[chi - off - 1] = CHUUNIT[4];
//chu[chi - off - 2] = CHU[str[sti - 2] - 48];
//chu[chi - off - 3] = CHUUNIT[5];
//chu[chi - off - 4] = CHU[str[sti - 3] - 48];
//chu[chi - off - 5] = CHUUNIT[6];
chu[chi - off - 1] = CHU[str[sti - 4] - 48]; if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off - 2] = CHLUNIT[8];
chu[chi - off - 2] = CHUUNIT[8];
}
else
{
chl[chi - off - 2] = CHLUNIT[7];
chu[chi - off - 2] = CHUUNIT[7];
}
off -= 5;
}
break;
default://1111
{
if (i == 0 && rem == 0)
{
break;//如果余数为0且当前为最后,则跳出
} if ((i >> 1 << 1) == i)//i % 2 == 0
{
chl[chi - off/**/] = CHLUNIT[8];
chu[chi - off/**/] = CHUUNIT[8];
}
else
{
chl[chi - off/**/] = CHLUNIT[7];
chu[chi - off/**/] = CHUUNIT[7];
}
off -= 7;
}
break;
}
} if (enu == 0xF && (quo >> 1 << 1) != quo)
{
off--;//上次所有位为0,并且高位为万的时候,清除'万'字
}
//循环完成以后累加剩下的值
switch (rem)
{
case 1:
{
chl[chi - off] = CHL[str[sti - 1] - 48];
chu[chi - off] = CHU[str[sti - 1] - 48];
}
break;
case 2:
{
if (str[sti - 1] == '0')
{
//chl[chi - off/**/] = CHL[str[sti - 1] - 48];
chl[chi - off/**/] = CHLUNIT[4];
chl[chi - off - 1] = CHL[str[sti - 2] - 48]; //chu[chi - off/**/] = CHU[str[sti - 1] - 48];
chu[chi - off/**/] = CHUUNIT[4];
chu[chi - off - 1] = CHU[str[sti - 2] - 48];
off--;
}
else
{
chl[chi - off/**/] = CHL[str[sti - 1] - 48];
chl[chi - off - 1] = CHLUNIT[4];
chl[chi - off - 2] = CHL[str[sti - 2] - 48]; chu[chi - off/**/] = CHU[str[sti - 1] - 48];
chu[chi - off - 1] = CHUUNIT[4];
chu[chi - off - 2] = CHU[str[sti - 2] - 48];
}
}
break;
case 3:
{
if (str[sti - 1] == '0')
{
if (str[sti - 2] == '0')
{
//chl[chi - off/**/] = CHL[str[sti - 1] - 48];
//chl[chi - off - 1] = CHLUNIT[4];
//chl[chi - off - 2] = CHL[str[sti - 2] - 48];
chl[chi - off/**/] = CHLUNIT[5];
chl[chi - off - 1] = CHL[str[sti - 3] - 48]; //chu[chi - off/**/] = CHU[str[sti - 1] - 48];
//chu[chi - off - 1] = CHUUNIT[4];
//chu[chi - off - 2] = CHU[str[sti - 2] - 48];
chu[chi - off/**/] = CHUUNIT[5];
chu[chi - off - 1] = CHU[str[sti - 3] - 48];
off -= 3;
}
else
{
//chl[chi - off/**/] = CHL[str[sti - 1] - 48];
chl[chi - off/**/] = CHLUNIT[4];
chl[chi - off - 1] = CHL[str[sti - 2] - 48];
chl[chi - off - 2] = CHLUNIT[5];
chl[chi - off - 3] = CHL[str[sti - 3] - 48]; //chu[chi - off/**/] = CHU[str[sti - 1] - 48];
chu[chi - off/**/] = CHUUNIT[4];
chu[chi - off - 1] = CHU[str[sti - 2] - 48];
chu[chi - off - 2] = CHUUNIT[5];
chu[chi - off - 3] = CHU[str[sti - 3] - 48];
off--;
}
}
else
{
if (str[sti - 2] == '0')
{
chl[chi - off/**/] = CHL[str[sti - 1] - 48];
//chl[chi - off - 1] = CHLUNIT[4];
chl[chi - off - 1] = CHL[str[sti - 2] - 48];
chl[chi - off - 2] = CHLUNIT[5];
chl[chi - off - 3] = CHL[str[sti - 3] - 48]; chu[chi - off/**/] = CHU[str[sti - 1] - 48];
//chu[chi - off - 1] = CHUUNIT[4];
chu[chi - off - 1] = CHU[str[sti - 2] - 48];
chu[chi - off - 2] = CHUUNIT[5];
chu[chi - off - 3] = CHU[str[sti - 3] - 48];
off--;
}
else
{
chl[chi - off/**/] = CHL[str[sti - 1] - 48];
chl[chi - off - 1] = CHLUNIT[4];
chl[chi - off - 2] = CHL[str[sti - 2] - 48];
chl[chi - off - 3] = CHLUNIT[5];
chl[chi - off - 4] = CHL[str[sti - 3] - 48]; chu[chi - off/**/] = CHU[str[sti - 1] - 48];
chu[chi - off - 1] = CHUUNIT[4];
chu[chi - off - 2] = CHU[str[sti - 2] - 48];
chu[chi - off - 3] = CHUUNIT[5];
chu[chi - off - 4] = CHU[str[sti - 3] - 48];
}
}
}
break;
default: //0
{ }
break;
}
//剩下的值计算完成之后.附加符号
if (str[0] == '-')
{
chl[0] = SIGNED[chi - off];//'负'
chu[0] = SIGNED[chi - off];//'负'
}
this._Lower = new string(chl, 2 - off, chl.Length - (2 - off));//当初定义的偏移量为2
this._Upper = new string(chu, 2 - off, chu.Length - (2 - off));
#endregion 转换细节 }
else if (level == 10)
{
int dot = str.LastIndexOf('.');
if (dot != -1)
{
//除去小数(decimal.Truncate()的效率比处理字符串效率要高,所以这里我们就不必要自己处理了.)
CHMoney temp = new CHMoney(decimal.Truncate(((IConvertible)src).ToDecimal(null)), 9);
if(str.Length - dot > 2)
{
if (str[dot + 1] == '0')
{
if (str[dot + 2] == '0')
{
this = temp;//小数点后2位都为0
}
else
{
char[] lower = new char[temp._Lower.Length + 2];//占用'整'字,之后再需要2个字符空间
char[] upper = new char[lower.Length]; for (int i = 0; i < temp._Lower.Length; i++)
{
lower[i] = temp._Lower[i];
upper[i] = temp._Upper[i];
}
lower[lower.Length - 3] = CHL[str[dot + 1] - 48];
//lower[lower.Length - 3] = CHLUNIT[2];
lower[lower.Length - 2] = CHL[str[dot + 2] - 48];
lower[lower.Length - 1] = CHLUNIT[1]; upper[upper.Length - 3] = CHU[str[dot + 1] - 48];
//upper[upper.Length - 3] = CHUUNIT[2];
upper[upper.Length - 2] = CHU[str[dot + 2] - 48];
upper[upper.Length - 1] = CHUUNIT[1]; this._Lower = new string(lower);
this._Upper = new string(upper);
return;
}
}
else
{
goto Label_0011;
}
}
else
{
goto Label_0011;
}
Label_0011:
if (str[dot + 1] == '0')
{
this = temp;//小数点后只有1位且为0
}
else
{
char[] lower = new char[temp._Lower.Length + 3];//占用'整'字,之后再需要3个字符空间
char[] upper = new char[lower.Length]; for (int i = 0; i < temp._Lower.Length; i++)
{
lower[i] = temp._Lower[i];
upper[i] = temp._Upper[i];
}
lower[lower.Length - 4] = CHL[str[dot + 1] - 48];
lower[lower.Length - 3] = CHLUNIT[2];
lower[lower.Length - 2] = CHL[str[dot + 2] - 48];
lower[lower.Length - 1] = CHLUNIT[1]; upper[upper.Length - 4] = CHU[str[dot + 1] - 48];
upper[upper.Length - 3] = CHUUNIT[2];
upper[upper.Length - 2] = CHU[str[dot + 2] - 48];
upper[upper.Length - 1] = CHUUNIT[1]; this._Lower = new string(lower);
this._Upper = new string(upper);
}
}
else
{
this = new CHMoney(src, 9);//如果没有小数点.
}
}
else if (level == 13)
{
this = new CHMoney(str);//调用string
}
else
{
int e = str.LastIndexOf('E');
int dot = str.LastIndexOf('.');
if(e != -1)
{
//科学计数法
int power = int.TryParse(str.Substring(e + 2), out power) ? power : -1;//这里不排除捣蛋鬼会使Substring出现异常..
if (power != -1)
{
char[] res;
if (dot == -1)
{
power = power - e + 1;
res = new char[e + power];
for (int i = 0; i < e; i++)
{
res[i] = str[i];
}
for (int i = e; i < res.Length; i++)
{
res[i] = '0';
}
}
else
{
power = power - e + dot + 1;//应该除去小数点之后增加几个'0'?
res = new char[e + power - 1];//长度应除去小数点
for (int i = 0; i < e - 1; i++)
{
if (i < dot)
{
res[i] = str[i];
}
else
{
res[i] = str[i + 1];
}
}
for (int i = e-1; i < res.Length; i++)
{
res[i] = '0';
}
}
this = new CHMoney(new string(res), 9);
}
else
{
//这里应该不会到达,除非强制性使用反射,调用次方法传递错误对象.
throw new FormatException("Why is there an format-exception?");
}
}
else
{
//非科学计数法
this = new CHMoney(str, 10);
}
}
}
/// <summary>
/// 从Boolean值创建一个CHMoney
/// </summary>
/// <param name="src">布尔值</param>
public CHMoney(bool src)
{
this = new CHMoney(src, 0);
}
public CHMoney(sbyte src)
{
this = new CHMoney(src, 1);
}
public CHMoney(byte src)
{
this = new CHMoney(src, 2);
}
public CHMoney(Int16 src)
{
this = new CHMoney(src, 3);
}
public CHMoney(char src)
{
this = new CHMoney(src, 4);
}
public CHMoney(UInt16 src)
{
this = new CHMoney(src, 5);
}
public CHMoney(Int32 src)
{
this = new CHMoney(src, 6);
}
public CHMoney(UInt32 src)
{
this = new CHMoney(src, 7);
}
public CHMoney(Int64 src)
{
this = new CHMoney(src, 8);
}
public CHMoney(UInt64 src)
{
this = new CHMoney(src, 9);
}
public CHMoney(Decimal src)
{
this = new CHMoney(src, 10);
}
public CHMoney(Single src)
{
this = new CHMoney(src, 11);
}
public CHMoney(Double src)
{
this = new CHMoney(src, 12);
}
#endregion 构造函数 #region 隐式转换
public static implicit operator CHMoney(bool src)
{
return new CHMoney(src);
}
public static implicit operator CHMoney(byte src)
{
return new CHMoney(src);
}
public static implicit operator CHMoney(sbyte src)
{
return new CHMoney(src);
}
public static implicit operator CHMoney(char src)
{
return new CHMoney(src);
}
public static implicit operator CHMoney(string src)
{
return new CHMoney(src);
}
public static implicit operator CHMoney(Int16 src)
{
return new CHMoney(src);
}
public static implicit operator CHMoney(UInt16 src)
{
return new CHMoney(src);
}
public static implicit operator CHMoney(Int32 src)
{
return new CHMoney(src);
}
public static implicit operator CHMoney(UInt32 src)
{
return new CHMoney(src);
}
public static implicit operator CHMoney(Int64 src)
{
return new CHMoney(src);
}
public static implicit operator CHMoney(UInt64 src)
{
return new CHMoney(src);
}
public static implicit operator CHMoney(Single src)
{
return new CHMoney(src);
}
public static implicit operator CHMoney(Double src)
{
return new CHMoney(src);
}
public static implicit operator CHMoney(Decimal src)
{
return new CHMoney(src);
}
//导出
public static explicit operator Boolean(CHMoney val)
{
return ((IConvertible)val._Source).ToBoolean(null);
}
public static explicit operator Byte(CHMoney val)
{
return ((IConvertible)val._Source).ToByte(null);
}
public static explicit operator SByte(CHMoney val)
{
return ((IConvertible)val._Source).ToSByte(null);
}
public static explicit operator Char(CHMoney val)
{
return ((IConvertible)val._Source).ToChar(null);
}
public static explicit operator String(CHMoney val)
{
return ((IConvertible)val._Source).ToString(null);
}
public static explicit operator Int16(CHMoney val)
{
return ((IConvertible)val._Source).ToInt16(null);
}
public static explicit operator UInt16(CHMoney val)
{
return ((IConvertible)val._Source).ToUInt16(null);
}
public static explicit operator Int32(CHMoney val)
{
return ((IConvertible)val._Source).ToInt32(null);
}
public static explicit operator UInt32(CHMoney val)
{
return ((IConvertible)val._Source).ToUInt32(null);
}
public static explicit operator Int64(CHMoney val)
{
return ((IConvertible)val._Source).ToInt64(null);
}
public static explicit operator UInt64(CHMoney val)
{
return ((IConvertible)val._Source).ToUInt64(null);
}
public static explicit operator Single(CHMoney val)
{
return ((IConvertible)val._Source).ToSingle(null);
}
public static explicit operator Double(CHMoney val)
{
return ((IConvertible)val._Source).ToDouble(null);
}
public static explicit operator Decimal(CHMoney val)
{
return ((IConvertible)val._Source).ToDecimal(null);
} #endregion 隐式转换 #region 操作符重载
/// <summary>
/// 比较两个值是否相等
/// </summary>
/// <param name="x">第一个值</param>
/// <param name="y">第二个值</param>
/// <returns>如果x等于y则返回true</returns>
public static bool operator ==(CHMoney x, CHMoney y)
{
return x._Source == y._Source;
}
/// <summary>
/// 比较两个值是否不相等
/// </summary>
/// <param name="x">第一个值</param>
/// <param name="y">第二个值</param>
/// <returns>如果x不等于y则返回true</returns>
public static bool operator !=(CHMoney x, CHMoney y)
{
return x._Source != y._Source;
}
/// <summary>
/// 比较两个值
/// </summary>
/// <param name="x">第一个值</param>
/// <param name="y">第二个值</param>
/// <returns>如果x大于或等于y则返回true</returns>
public static bool operator >=(CHMoney x, CHMoney y)
{
return x - y >= 0;
}
/// <summary>
/// 比较两个值
/// </summary>
/// <param name="x">第一个值</param>
/// <param name="y">第二个值</param>
/// <returns>如果x小于或等于y则返回true</returns>
public static bool operator <=(CHMoney x, CHMoney y)
{
return x - y <= 0; ;
}
/// <summary>
/// 比较两个值
/// </summary>
/// <param name="x">第一个值</param>
/// <param name="y">第二个值</param>
/// <returns>如果x大于y则返回true</returns>
public static bool operator >(CHMoney x, CHMoney y)
{
return x - y > 0;
}
/// <summary>
/// 比较两个值
/// </summary>
/// <param name="x">第一个值</param>
/// <param name="y">第二个值</param>
/// <returns>如果x小于y则返回true</returns>
public static bool operator <(CHMoney x, CHMoney y)
{
return x - y < 0;
}
/// <summary>
/// 求正
/// </summary>
/// <param name="x">操作对象</param>
/// <returns>返回本身</returns>
/// <exception cref="NotImplementedException">对布尔对象(Boolean)使用求正运算符会引发此异常</exception>
public static CHMoney operator +(CHMoney x)
{
if (x._Source == null)
{
x.Init();
} Type xt = x._Source.GetType();
if (xt == TYPE[0])
{
throw new NotImplementedException("This operator can not be applied to boolean type");
}
return x;
}
/// <summary>
/// 求负
/// </summary>
/// <param name="x">操作对象</param>
/// <returns>返回(0-x)</returns>
/// <exception cref="OverflowException">运算溢出异常</exception>
/// <exception cref="NotImplementedException">对布尔对象(Boolean)使用求负运算符会引发此异常</exception>
public static CHMoney operator -(CHMoney x)
{
try
{
//return 0 - x;//最小负数总比最大正数的绝对值大1.当被减数为最小负数(相对与变量能存储的值)时,会出现溢出.
return -1 * x;//这个是系统推荐的正确做法,虽然上面的做法效率更高.
}
catch (OverflowException ex)
{
throw new OverflowException(ex.Message);
}
catch (NotImplementedException ex)
{
throw new NotImplementedException(ex.Message);
}
}
/// <summary>
/// 进行加法运算
/// </summary>
/// <param name="x">被加数</param>
/// <param name="y">加数</param>
/// <returns>返回运算后的结果</returns>
/// <exception cref="OverflowException">运算溢出异常</exception>
/// <exception cref="NotImplementedException">对布尔对象(Boolean)使用加法运算符会引发此异常</exception>
public static CHMoney operator +(CHMoney x, CHMoney y)
{
if (x._Source == null)
{
x.Init();
}
if (y._Source == null)
{
y.Init();
} Type xt = x._Source.GetType();
Type yt = y._Source.GetType(); //哇,Boolean相加,结果很纠结.算了,给个异常吧.
if (xt == TYPE[0] || yt == TYPE[0])
{
throw new NotImplementedException("This operator can not be applied to boolean type");
} int level = 0; for (int i=0; i < TYPE.Length; i++)
{
if (xt == TYPE[i] || yt == TYPE[i])
{
level = i;
}
}
try
{
if (level < 7)//如果在int范围内,则统一使用int类型
{
long xv = (long)x;
long yv = (long)y;
long res = xv + yv;
//如果相加后的结果超出范围
if (res < int.MinValue || res > int.MaxValue)
{
return new CHMoney(xv + yv);
}
else
{
return new CHMoney(((IConvertible)res).ToInt32(null));
}
}
else if (level < 10)//如果在long范围内,则统一使用long类型
{
decimal xv = (decimal)x;
decimal yv = (decimal)y;
decimal res = xv + yv;
//如果相加后的结果超出范围
if (res < long.MinValue || res > long.MaxValue)
{
return new CHMoney(xv + yv);
}
else
{
return new CHMoney(((IConvertible)res).ToInt64(null));
}
}
else if (level == 10)//如果最大级别使用了十进制则,双方都使用十进制
{
return new CHMoney((decimal)x + (decimal)y);//这里可能会出现溢出,所以在外面捕获了异常 //下面的代码为了提前检测是否出现溢出.但是溢出操作让系统来检测效率更好.
//这里不应该转换为Double,因为Double损失了精度.这个是我们不想看到的.
//decimal xv = (decimal)x;
//decimal yv = (decimal)y;
//if (xv < 0)
//{
// if (yv < 0)
// {
// if (decimal.MinValue - yv > xv)
// {
// throw new OverflowException("Addition operation result overflows");
// }
// }
//}
//else// if (xv >= 0)
//{
// if (yv > 0)
// {
// if (decimal.MaxValue - yv < xv)
// {
// throw new OverflowException("Addition operation result overflows");
// }
// }
//}
//return new CHMoney(xv + yv);
}
//如果任何一方使用了浮点运算,则默认使用Double结果.
return new CHMoney((double)x + (double)y);
}
catch (OverflowException ex)
{
throw new OverflowException(ex.Message);//double也要捕获溢出,抛出处理吧...
} }
/// <summary>
/// 进行减法运算
/// </summary>
/// <param name="x">被减数</param>
/// <param name="y">减数</param>
/// <returns>返回运算后的结果</returns>
/// <exception cref="OverflowException">运算溢出异常</exception>
/// <exception cref="NotImplementedException">对布尔对象(Boolean)使用减法运算符会引发此异常</exception>
public static CHMoney operator -(CHMoney x, CHMoney y)
{
if (x._Source == null)
{
x.Init();
}
if (y._Source == null)
{
y.Init();
} Type xt = x._Source.GetType();
Type yt = y._Source.GetType(); if (xt == TYPE[0] || yt == TYPE[0])
{
throw new NotImplementedException("This operator can not be applied to boolean type");
}
int level = 0; for (int i = 0; i < TYPE.Length; i++)
{
if (xt == TYPE[i] || yt == TYPE[i])
{
level = i;
}
} try
{
if (level < 7)//如果在int范围内,则统一使用int类型
{
long xv = (long)x;
long yv = (long)y;
long res = xv - yv;
//如果相减后的结果超出范围
if (res < int.MinValue || res > int.MaxValue)
{
return new CHMoney(res);
}
else
{
return new CHMoney(((IConvertible)res).ToInt32(null));
}
}
else if (level < 10)//如果在long范围内,则统一使用long类型
{
decimal xv = (decimal)x;
decimal yv = (decimal)y;
decimal res = xv - yv;
//如果相减后的结果超出范围
if (res < long.MinValue || res > long.MaxValue)
{
return new CHMoney(res);
}
else
{
return new CHMoney(((IConvertible)res).ToInt64(null));
}
}
else if (level == 10)
{
return new CHMoney((decimal)x - (decimal)y);//这里可能会出现溢出,所以在外面捕获了异常 //下面的代码为了提前检测是否出现溢出.但是溢出操作让系统来检测效率更好.
//这里不应该转换为Double,因为Double损失了精度.这个是我们不想看到的.
//decimal xv = (decimal)x;
//decimal yv = (decimal)y;
//if (xv < 0)
//{
// if (yv > 0)
// {
// if (decimal.MinValue + yv > xv)
// {
// throw new OverflowException("Addition operation result overflows");
// }
// }
//}
//else// if (xv >= 0)
//{
// if (yv < 0)
// {
// if (decimal.MaxValue + yv < xv)
// {
// throw new OverflowException("Addition operation result overflows");
// }
// }
//}
//return new CHMoney(xv + yv);
}
return new CHMoney((double)x - (double)y);
}
catch (OverflowException ex)
{
throw new OverflowException(ex.Message);//double也要捕获溢出,抛出处理吧...
} }
/// <summary>
/// 进行乘法运算
/// </summary>
/// <param name="x">被乘数</param>
/// <param name="y">乘数</param>
/// <returns>返回运算后的结果</returns>
/// <exception cref="OverflowException">运算溢出异常</exception>
/// <exception cref="NotImplementedException">对布尔对象(Boolean)使用乘法运算符会引发此异常</exception>
public static CHMoney operator *(CHMoney x, CHMoney y)
{
if (x._Source == null)
{
x.Init();
}
if (y._Source == null)
{
y.Init();
} Type xt = x._Source.GetType();
Type yt = y._Source.GetType(); if (xt == TYPE[0] || yt == TYPE[0])
{
throw new NotImplementedException("This operator can not be applied to boolean type");
}
int level = 0; for (int i = 0; i < TYPE.Length; i++)
{
if (xt == TYPE[i] || yt == TYPE[i])
{
level = i;
}
}
try
{
if (level < 7)//如果在int范围内,则统一使用int类型
{
//long xv = (long)x;
//long yv = (long)y;
//long res = xv * yv;//这里int相乘可能会超出long值的范围 decimal xv = (decimal)x;
decimal yv = (decimal)y; decimal res = xv * yv;//如果这里都溢出了.就不进行计算了.抛出异常 //如果相乘后的结果超出范围
if (res < long.MinValue || res > long.MaxValue)
{
return new CHMoney(res);
}
else if (res < int.MinValue || res > int.MaxValue)
{
return new CHMoney(((IConvertible)res).ToInt64(null));
}
else
{
return new CHMoney(((IConvertible)res).ToInt32(null));
}
}
else if (level < 10)//如果在long范围内,则统一使用long类型
{
decimal xv = (decimal)x;
decimal yv = (decimal)y;
decimal res = xv * yv;
//如果相乘后的结果超出范围
if (res < long.MinValue || res > long.MaxValue)
{
return new CHMoney(res);
}
else
{
return new CHMoney(((IConvertible)res).ToInt64(null));
}
}
else if (level == 10)//如果最大级别使用了十进制则,双方都使用十进制
{
//这里可能会出现溢出,所以在外面捕获了异常
return new CHMoney((decimal)x * (decimal)y);
}
//如果任何一方使用了浮点运算,则默认使用Double结果.
return new CHMoney((double)x * (double)y);
}
catch (OverflowException ex)
{
throw new OverflowException(ex.Message);
}
}
/// <summary>
/// 进行除法运算
/// </summary>
/// <param name="x">被除数</param>
/// <param name="y">除数</param>
/// <returns>返回运算后的结果</returns>
/// <exception cref="OverflowException">运算溢出异常</exception>
/// <exception cref="DivideByZeroException">使用了整形数字(0)作为除数引发的异常</exception>
/// <exception cref="NotImplementedException">对布尔对象(Boolean)使用除法运算符会引发此异常</exception>
public static CHMoney operator /(CHMoney x, CHMoney y)
{
if (x._Source == null)
{
x.Init();
}
if (y._Source == null)
{
y.Init();
} Type xt = x._Source.GetType();
Type yt = y._Source.GetType(); if (xt == TYPE[0] || yt == TYPE[0])
{
throw new NotImplementedException("This operator can not be applied to boolean type");
}
int level = 0; for (int i = 0; i < TYPE.Length; i++)
{
if (xt == TYPE[i] || yt == TYPE[i])
{
level = i;
}
}
try
{
if (level < 7)//如果在int范围内,则统一使用int类型
{
//long xv = (long)x;
//long yv = (long)y;
//long res = xv / yv;//这里int相除可能会超出long值的范围 decimal xv = (decimal)x;
decimal yv = (decimal)y; decimal res = xv / yv;//如果这里都溢出了.就不进行计算了.抛出异常 //如果相除后的结果超出范围
if (res < long.MinValue || res > long.MaxValue)
{
return new CHMoney(res);
}
else if (res < int.MinValue || res > int.MaxValue)
{
return new CHMoney(((IConvertible)res).ToInt64(null));
}
else
{
return new CHMoney(((IConvertible)res).ToInt32(null));
}
}
else if (level < 10)//如果在long范围内,则统一使用long类型
{
decimal xv = (decimal)x;
decimal yv = (decimal)y;
decimal res = xv / yv;
//如果相除后的结果超出范围
if (res < long.MinValue || res > long.MaxValue)
{
return new CHMoney(res);
}
else
{
return new CHMoney(((IConvertible)res).ToInt64(null));
}
}
else if (level == 10)//如果最大级别使用了十进制则,双方都使用十进制
{
//这里可能会出现溢出,所以在外面捕获了异常
return new CHMoney((decimal)x / (decimal)y);
}
//如果任何一方使用了浮点运算,则默认使用Double结果.
return new CHMoney((double)x / (double)y);
}
catch (OverflowException ex)
{
throw new OverflowException(ex.Message);
}
catch (DivideByZeroException ex)
{
throw new DivideByZeroException(ex.Message);//在进行整数或十进制运算时使用了0作为除数.
} }
/// <summary>
/// 进行求模运算
/// </summary>
/// <param name="x">被除数</param>
/// <param name="y">除数</param>
/// <returns>返回运算后的结果</returns>
/// <exception cref="DivideByZeroException">使用了整形数字(0)作为除数引发的异常</exception>
/// <exception cref="NotImplementedException">对布尔对象(Boolean)使用求模运算符会引发此异常</exception>
public static CHMoney operator %(CHMoney x, CHMoney y)
{
if (x._Source == null)
{
x.Init();
}
if (y._Source == null)
{
y.Init();
} Type xt = x._Source.GetType();
Type yt = y._Source.GetType(); if (xt == TYPE[0] || yt == TYPE[0])
{
throw new NotImplementedException("This operator can not be applied to boolean type");
}
int level = 0; for (int i = 0; i < TYPE.Length; i++)
{
if (xt == TYPE[i] || yt == TYPE[i])
{
level = i;
}
}
try
{
if (level < 7)//如果在int范围内,则统一使用int类型
{
return new CHMoney((int)x % (int)y);
}
else if (level < 10)//如果在long范围内,则统一使用long类型
{
decimal xv = (decimal)x;
decimal yv = (decimal)y;
decimal res = xv % yv;
//如果相除后的结果超出范围
if (res < long.MinValue || res > long.MaxValue)
{
return new CHMoney(res);
}
else
{
return new CHMoney(((IConvertible)res).ToInt64(null));
}
}
else if (level == 10)//如果最大级别使用了十进制则,双方都使用十进制
{
return new CHMoney((decimal)x % (decimal)y);
}
//如果任何一方使用了浮点运算,则默认使用Double结果.
return new CHMoney((double)x % (double)y);
}
//catch (OverflowException ex)
//{
// throw new OverflowException(ex.Message);//溢出异常不会出现吧?
//}
catch (DivideByZeroException ex)
{
throw new DivideByZeroException(ex.Message);//在进行整数或十进制运算时使用了0作为除数.(这里求模使用了0当做除数)
} }
/// <summary>
/// 进行自增操作
/// </summary>
/// <param name="x">操作的对象</param>
/// <returns>返回运算后的结果</returns>
/// <exception cref="OverflowException">运算溢出异常</exception>
public static CHMoney operator ++(CHMoney x)
{
if (x._Source == null)
{
x.Init();
} Type xt = x._Source.GetType(); if (xt == TYPE[0])
{
throw new NotImplementedException("This operator can not be applied to boolean type");
}
int level = 0; for (int i = 0; i < TYPE.Length; i++)
{
if (xt == TYPE[i])
{
level = i;
break;
}
}
try
{
if (level < 7)//如果在int范围内,则统一使用int类型
{
long res = (long)x + 1L; //如果自增后的结果超出范围
if (res < int.MinValue || res > int.MaxValue)
{
return new CHMoney(res);
}
else
{
return new CHMoney(((IConvertible)res).ToInt32(null));
}
}
else if (level < 10)//如果在long范围内,则统一使用long类型
{
decimal res = (decimal)x + 1M;
//如果自增后的结果超出范围
if (res < long.MinValue || res > long.MaxValue)
{
return new CHMoney(res);
}
else
{
return new CHMoney(((IConvertible)res).ToInt64(null));
}
}
else if (level == 10)//如果最大级别使用了十进制则,双方都使用十进制
{
//这里可能会出现溢出,所以在外面捕获了异常
//其实这里可以直接判断如果等于decimal.MaxValue自增以后肯定会溢出,不过还是放弃了.
return new CHMoney((decimal)x +1M);
}
//如果任何一方使用了浮点运算,则默认使用Double结果.
return new CHMoney((double)x + 1D);
}
catch (OverflowException ex)
{
throw new OverflowException(ex.Message);
}
}
/// <summary>
/// 进行自减操作
/// </summary>
/// <param name="x">操作的对象</param>
/// <returns>返回运算后的结果</returns>
/// <exception cref="OverflowException">运算溢出异常</exception>
public static CHMoney operator --(CHMoney x)
{
if (x._Source == null)
{
x.Init();
} Type xt = x._Source.GetType(); if (xt == TYPE[0])
{
throw new NotImplementedException("This operator can not be applied to boolean type");
}
int level = 0; for (int i = 0; i < TYPE.Length; i++)
{
if (xt == TYPE[i])
{
level = i;
break;
}
}
try
{
if (level < 7)//如果在int范围内,则统一使用int类型
{
long res = (long)x - 1L; //如果自减后的结果超出范围
if (res < int.MinValue || res > int.MaxValue)
{
return new CHMoney(res);
}
else
{
return new CHMoney(((IConvertible)res).ToInt32(null));
}
}
else if (level < 10)//如果在long范围内,则统一使用long类型
{
decimal res = (decimal)x - 1M;
//如果自减后的结果超出范围
if (res < long.MinValue || res > long.MaxValue)
{
return new CHMoney(res);
}
else
{
return new CHMoney(((IConvertible)res).ToInt64(null));
}
}
else if (level == 10)//如果最大级别使用了十进制则,双方都使用十进制
{
//这里可能会出现溢出,所以在外面捕获了异常
return new CHMoney((decimal)x - 1M);
}
//如果任何一方使用了浮点运算,则默认使用Double结果.
return new CHMoney((double)x - 1D);
}
catch (OverflowException ex)
{
throw new OverflowException(ex.Message);//溢出,抛出吧,反正都超出值类型的控范围了,就不管了
}
}
/// <summary>
/// 进行按位求补操作,仅对整形(Byte,SByte,Char,Int16,Uint16,Int32,Uint32,Int64,Uint64)有效,否则将引发异常
/// </summary>
/// <param name="x">操作的对象</param>
/// <returns>返回运算后的结果</returns>
/// <exception cref="NotImplementedException">对非整形对象(Boolean,Float,Double,Decimal)使用按位求补运算符会引发此异常</exception>
public static CHMoney operator ~(CHMoney x)
{
if (x._Source == null)
{
x.Init();
} Type xt = x._Source.GetType(); int level = 0; for (int i = 0; i < TYPE.Length; i++)
{
if (xt == TYPE[i])
{
level = i;
break;
}
}
if (level == 0 || level > 9)
{
throw new NotImplementedException("This operator can not be applied to " + xt.Name.ToLower() + " type");
}
else if (level < 7)//如果在int范围内,则统一使用int类型
{
return new CHMoney(~(int)x);
}
else if (level == 7)
{
return new CHMoney(~(uint)x);
}
else if (level == 8)
{
return new CHMoney(~(long)x);
}
else
{
return new CHMoney(~(ulong)x);
}
}
/// <summary>
/// 进行求反操作,仅对布尔(Boolean)有效,否则将引发异常
/// </summary>
/// <param name="x">操作的对象</param>
/// <returns>返回运算后的结果</returns>
/// <exception cref="NotImplementedException">对非Boolean类型使用此操作符,否则将引发异常</exception>
public static CHMoney operator !(CHMoney x)
{
if (x._Source == null)
{
x.Init();
} Type xt = x._Source.GetType(); if (xt == TYPE[0])
{
return new CHMoney(!(bool)x);
}
else
{
throw new NotImplementedException("This operator can not be applied to " + xt.Name.ToLower() + " type");
}
} /// <summary>
/// 进行按位求与操作,仅对整形(Byte,SByte,Char,Int16,Uint16,Int32,Uint32,Int64,Uint64)和布尔(Boolean)有效,否则将引发异常
/// </summary>
/// <param name="x">操作的对象</param>
/// <param name="y">操作的对象</param>
/// <returns>返回运算后的结果</returns>
/// <exception cref="NotImplementedException">对非整形和布尔对象(Float,Double,Decimal)使用按位求反运算符会引发此异常</exception>
public static CHMoney operator &(CHMoney x, CHMoney y)
{
if (x._Source == null)
{
x.Init();
}
if (y._Source == null)
{
y.Init();
} Type xt = x._Source.GetType();
Type yt = y._Source.GetType(); int level = 0; for (int i = 0; i < TYPE.Length; i++)
{
if (xt == TYPE[i] || yt == TYPE[i])
{
level = i;
}
}
if (level > 9)
{
throw new NotImplementedException("This operator can not be applied to " + xt.Name.ToLower() + " type");
}
else if (level == 0)
{
return new CHMoney((bool)x & (bool)y);
}
else if (level < 7)//如果在int范围内,则统一使用int类型
{
return new CHMoney((int)x & (int)y);
}
else if (level == 7)
{
return new CHMoney((uint)x & (uint)y);
}
else if (level == 8)
{
return new CHMoney((long)x & (long)y);
}
else
{
return new CHMoney((ulong)x & (ulong)y);
} } /// <summary>
/// 进行按位求或操作,仅对整形(Byte,SByte,Char,Int16,Uint16,Int32,Uint32,Int64,Uint64)和布尔(Boolean)有效,否则将引发异常
/// </summary>
/// <param name="x">操作的对象</param>
/// <param name="y">操作的对象</param>
/// <returns>返回运算后的结果</returns>
/// <exception cref="NotImplementedException">对非整形和布尔对象(Float,Double,Decimal)使用按位求或运算符会引发此异常</exception>
public static CHMoney operator |(CHMoney x, CHMoney y)
{
if (x._Source == null)
{
x.Init();
}
if (y._Source == null)
{
y.Init();
} Type xt = x._Source.GetType();
Type yt = y._Source.GetType(); int level = 0; for (int i = 0; i < TYPE.Length; i++)
{
if (xt == TYPE[i] || yt == TYPE[i])
{
level = i;
}
}
if (level > 9)
{
throw new NotImplementedException("This operator can not be applied to " + xt.Name.ToLower() + " type");
}
else if (level == 0)
{
return new CHMoney((bool)x | (bool)y);
}
else if (level < 7)//如果在int范围内,则统一使用int类型
{
return new CHMoney((int)x | (int)y);
}
else if (level == 7)
{
return new CHMoney((uint)x | (uint)y);
}
else if (level == 8)
{
return new CHMoney((long)x | (long)y);
}
else
{
return new CHMoney((ulong)x | (ulong)y);
} }
/// <summary>
/// 进行按位异或操作,仅对整形(Byte,SByte,Char,Int16,Uint16,Int32,Uint32,Int64,Uint64)和布尔(Boolean)有效,否则将引发异常
/// </summary>
/// <param name="x">操作的对象</param>
/// <param name="y">操作的对象</param>
/// <returns>返回运算后的结果</returns>
/// <exception cref="NotImplementedException">对非整形和布尔对象(Float,Double,Decimal)使用按位异或运算符会引发此异常</exception>
public static CHMoney operator ^(CHMoney x, CHMoney y)
{
if (x._Source == null)
{
x.Init();
}
if (y._Source == null)
{
y.Init();
} Type xt = x._Source.GetType();
Type yt = y._Source.GetType(); int level = 0; for (int i = 0; i < TYPE.Length; i++)
{
if (xt == TYPE[i] || yt == TYPE[i])
{
level = i;
}
}
if (level > 9)
{
throw new NotImplementedException("This operator can not be applied to " + xt.Name.ToLower() + " type");
}
else if (level == 0)
{
return new CHMoney((bool)x ^ (bool)y);
}
else if (level < 7)//如果在int范围内,则统一使用int类型
{
return new CHMoney((int)x ^ (int)y);
}
else if (level == 7)
{
return new CHMoney((uint)x ^ (uint)y);
}
else if (level == 8)
{
return new CHMoney((long)x ^ (long)y);
}
else
{
return new CHMoney((ulong)x ^ (ulong)y);
} }
/// <summary>
/// 进行左移位操作,仅对整形(Byte,SByte,Char,Int16,Uint16,Int32,Uint32,Int64,Uint64)有效,否则将引发异常
/// </summary>
/// <param name="x">操作的对象</param>
/// <param name="bit">左移的位数</param>
/// <returns>返回运算后的结果</returns>
/// <exception cref="NotImplementedException">对非整形对象(Boolean,Float,Double,Decimal)使用左移位运算符会引发此异常</exception>
public static CHMoney operator <<(CHMoney x, int bit)
{
if (x._Source == null)
{
x.Init();
} Type xt = x._Source.GetType(); int level = 0; for (int i = 0; i < TYPE.Length; i++)
{
if (xt == TYPE[i])
{
level = i;
break;
}
}
if (level == 0 || level > 9)
{
throw new NotImplementedException("This operator can not be applied to " + xt.Name.ToLower() + " type");
}
else if (level < 7)//如果在int范围内,则统一使用int类型
{
return new CHMoney((int)x << bit);
}
else if (level == 7)
{
return new CHMoney((uint)x << bit);
}
else if (level == 8)
{
return new CHMoney((long)x << bit);
}
else
{
return new CHMoney((ulong)x << bit);
}
}
/// <summary>
/// 进行右移位操作,仅对整形(Byte,SByte,Char,Int16,Uint16,Int32,Uint32,Int64,Uint64)有效,否则将引发异常
/// </summary>
/// <param name="x">操作的对象</param>
/// <param name="bit">右移的位数</param>
/// <returns>返回运算后的结果</returns>
/// <exception cref="NotImplementedException">对非整形对象(Boolean,Float,Double,Decimal)使用右移位运算符会引发此异常</exception>
public static CHMoney operator >>(CHMoney x, int bit)
{
if (x._Source == null)
{
x.Init();
} Type xt = x._Source.GetType(); int level = 0; for (int i = 0; i < TYPE.Length; i++)
{
if (xt == TYPE[i])
{
level = i;
break;
}
}
if (level == 0 || level > 9)
{
throw new NotImplementedException("This operator can not be applied to " + xt.Name.ToLower() + " type");
}
else if (level < 7)//如果在int范围内,则统一使用int类型
{
return new CHMoney((int)x >> bit);
}
else if (level == 7)
{
return new CHMoney((uint)x >> bit);
}
else if (level == 8)
{
return new CHMoney((long)x >> bit);
}
else
{
return new CHMoney((ulong)x >> bit);
}
}
#endregion 操作符重载 #region 方法重载
//所有方法暂时先不写
/// <summary>
/// 确定指定的 System.Object 是否等于当前的 System.Object。
/// </summary>
/// <param name="obj">与当前的 System.Object 进行比较的 System.Object。</param>
/// <returns>如果指定的 System.Object 等于当前的 System.Object,则为 true;否则为 false。</returns>
/// <exception cref="System.NullReferenceException">obj 参数为 null。</exception>
public override bool Equals(object obj)
{
//if (obj == null) return false;
//if (this._Source == null)
//{
// Init();
//}
//return this._Source.Equals(obj);
return base.Equals(obj);
}
/// <summary>
/// 用作特定类型的哈希函数。
/// </summary>
/// <returns>当前 System.Object 的哈希代码。</returns>
public override int GetHashCode()
{
//if (this._Source == null)
//{
// Init();
//}
//return this._Source.GetHashCode();
return base.GetHashCode();
}
/// <summary>
/// 返回表示当前 System.Object 的 System.String。
/// </summary>
/// <returns>System.String,表示当前的 System.Object。</returns>
public override string ToString()
{
//if (this._Lower == null)
//{
// Init();
//}
//return this.ToLower();
//return base.ToString();
if (this._Source == null)
{
Init();
}
return this._Source.ToString();
}
#endregion 方法重载 #region IComparable 成员 public int CompareTo(object obj)
{
if (obj == null)
{
return 1;
}
if (!(obj is CHMoney))
{
throw new ArgumentException("Object type must be CHMoney");
}
if (this._Source == null)
{
Init();
}
return this > (CHMoney)obj ? 1 : this < (CHMoney)obj ? -1 : 0;
} #endregion #region IFormattable 成员 public string ToString(string format)
{
return ToString(format, null);
} public string ToString(string format, IFormatProvider provider)
{
if (this._Source == null)
{
Init();
}
return ((IFormattable)this._Source).ToString(format, provider);
}
#endregion #region IConvertible 成员 public TypeCode GetTypeCode()
{
if (this._Source == null)
{
Init();
}
return ((IConvertible)this._Source).GetTypeCode();
} bool IConvertible.ToBoolean(IFormatProvider provider)
{
//return this != 0;
//return this > 0 || this < 0;
//return (bool)this;
return ((IConvertible)this._Source).ToBoolean(provider);
} byte IConvertible.ToByte(IFormatProvider provider)
{
//return (byte)this;
//想着更全面展现出所有转换过程,但是值类型带的转换很好用就直接用了.
//这段时间忙,过完这段,把.net2.0里面所有的函数源代码都公布出来给大家学习.
return ((IConvertible)this._Source).ToByte(provider);
} char IConvertible.ToChar(IFormatProvider provider)
{
//return (char)this;
return ((IConvertible)this._Source).ToChar(provider);
} DateTime IConvertible.ToDateTime(IFormatProvider provider)
{
return ((IConvertible)this._Source).ToDateTime(provider);
} decimal IConvertible.ToDecimal(IFormatProvider provider)
{
//return (decimal)this;
return ((IConvertible)this._Source).ToDecimal(provider);
} double IConvertible.ToDouble(IFormatProvider provider)
{
//return (double)this;
return ((IConvertible)this._Source).ToDouble(provider);
} short IConvertible.ToInt16(IFormatProvider provider)
{
//return (short)this;
return ((IConvertible)this._Source).ToInt16(provider);
} int IConvertible.ToInt32(IFormatProvider provider)
{
//return (int)this;
return ((IConvertible)this._Source).ToInt32(provider);
} long IConvertible.ToInt64(IFormatProvider provider)
{
//return (long)this;
return ((IConvertible)this._Source).ToInt64(provider);
} sbyte IConvertible.ToSByte(IFormatProvider provider)
{
//return (sbyte)this;
return ((IConvertible)this._Source).ToSByte(provider);
} float IConvertible.ToSingle(IFormatProvider provider)
{
//return (float)this;
return ((IConvertible)this._Source).ToSingle(provider);
} string IConvertible.ToString(IFormatProvider provider)
{
//return (string)this;
return ((IConvertible)this._Source).ToString(provider);
} object IConvertible.ToType(Type conversionType, IFormatProvider provider)
{
return ((IConvertible)this._Source).ToType(conversionType, provider);
} ushort IConvertible.ToUInt16(IFormatProvider provider)
{
//return (ushort)this;
return ((IConvertible)this._Source).ToUInt16(provider);
} uint IConvertible.ToUInt32(IFormatProvider provider)
{
//return (uint)this;
return ((IConvertible)this._Source).ToUInt32(provider);
} ulong IConvertible.ToUInt64(IFormatProvider provider)
{
//return (ulong)this;
return ((IConvertible)this._Source).ToUInt64(provider);
} #endregion #region IComparable<CHMoney> 成员 public int CompareTo(CHMoney other)
{
if (this._Source == null)
{
Init();
}
return this > other ? 1 : this < other ? -1 : 0;
} #endregion #region IEquatable<CHMoney> 成员 public bool Equals(CHMoney other)
{
if (this._Source == null)
{
Init();
}
return this._Source.Equals(other._Source);
} #endregion
}
放了啊, 我博客里 全部都有 直接copy