我正在尝试将一个小数位安全地转换成一个整数.
就像是
- public static bool Decimal.TryConvertToInt32(decimal val,out int val)
如果它不能转换为整数,则返回false,如果可以,则返回true或成功输出.
解决方法
这里:
- public static bool TryConvertToInt32(decimal val,out int intval)
- {
- if (val > int.MaxValue || val < int.MinValue)
- {
- intval = 0; // assignment required for out parameter
- return false;
- }
- intval = Decimal.ToInt32(val);
- return true;
- }