ConvertTo.cs 强制转换类




  1 None.gif using  System;
  2 None.gif using  System.Collections.Generic;
  3 None.gif using  System.Text;
  4 None.gif
  5 None.gif namespace  ChinaValue.CommonV2008
  6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  8InBlock.gif    /// 将字符串转换成任意其他数据
  9ExpandedSubBlockEnd.gif    /// </summary>

 10InBlock.gif    public static class ConvertStr
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 13InBlock.gif        /// 将字符串转换为Int16
 14InBlock.gif        /// </summary>
 15InBlock.gif        /// <param name="str"></param>
 16ExpandedSubBlockEnd.gif        /// <returns></returns>

 17InBlock.gif        public static Int16 ToInt16(String str, Int16 defValue)
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 19InBlock.gif            Int16 outValue;
 20InBlock.gif
 21InBlock.gif            if (!String.IsNullOrEmpty(str))
 22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 23InBlock.gif                if (Int16.TryParse(str, out outValue))
 24ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 25InBlock.gif                    return outValue;
 26ExpandedSubBlockEnd.gif                }

 27ExpandedSubBlockEnd.gif            }

 28InBlock.gif
 29InBlock.gif            return defValue;
 30ExpandedSubBlockEnd.gif        }

 31InBlock.gif        
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 33InBlock.gif        /// 将字符串转换为Int32
 34InBlock.gif        /// </summary>
 35InBlock.gif        /// <param name="str"></param>
 36ExpandedSubBlockEnd.gif        /// <returns></returns>

 37InBlock.gif        public static Int32 ToInt32(String str)
 38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 39InBlock.gif            return ToInt32(str, 0);
 40ExpandedSubBlockEnd.gif        }

 41InBlock.gif
 42ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 43InBlock.gif        /// 将类型转换为Int32
 44InBlock.gif        /// </summary>
 45InBlock.gif        /// <param name="obj">The obj.</param>
 46ExpandedSubBlockEnd.gif        /// <returns></returns>

 47InBlock.gif        public static Int32 ToInt32(object obj, Int32 defValue)
 48ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 49InBlock.gif            if (obj != null && obj != DBNull.Value)
 50ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 51InBlock.gif                return ToInt32(obj.ToString(), defValue);
 52ExpandedSubBlockEnd.gif            }

 53InBlock.gif            else
 54ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 55InBlock.gif                return defValue;
 56ExpandedSubBlockEnd.gif            }

 57ExpandedSubBlockEnd.gif        }

 58InBlock.gif
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 60InBlock.gif        /// 将字符串转换为Int32
 61InBlock.gif        /// </summary>
 62InBlock.gif        /// <param name="str"></param>
 63ExpandedSubBlockEnd.gif        /// <returns></returns>

 64InBlock.gif        public static Int32 ToInt32(String str, Int32 defValue)
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 66InBlock.gif            Int32 outValue;
 67InBlock.gif
 68InBlock.gif            if (!String.IsNullOrEmpty(str))
 69ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 70InBlock.gif                if (Int32.TryParse(str, out outValue))
 71ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 72InBlock.gif                    return outValue;
 73ExpandedSubBlockEnd.gif                }

 74ExpandedSubBlockEnd.gif            }

 75InBlock.gif
 76InBlock.gif            return defValue;
 77ExpandedSubBlockEnd.gif        }

 78InBlock.gif
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 80InBlock.gif        /// 将字符串转换为Int64
 81InBlock.gif        /// </summary>
 82InBlock.gif        /// <param name="str"></param>
 83ExpandedSubBlockEnd.gif        /// <returns></returns>

 84InBlock.gif        public static Int64 ToInt64(String str, Int64 defValue)
 85ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 86InBlock.gif            Int64 outValue;
 87InBlock.gif
 88InBlock.gif            if (!String.IsNullOrEmpty(str))
 89ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 90InBlock.gif                if (Int64.TryParse(str, out outValue))
 91ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 92InBlock.gif                    return outValue;
 93ExpandedSubBlockEnd.gif                }

 94ExpandedSubBlockEnd.gif            }

 95InBlock.gif
 96InBlock.gif            return defValue;
 97ExpandedSubBlockEnd.gif        }

 98InBlock.gif
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
100InBlock.gif        /// 将字符串转换为DateTime
101InBlock.gif        /// </summary>
102InBlock.gif        /// <param name="str"></param>
103ExpandedSubBlockEnd.gif        /// <returns></returns>

104InBlock.gif        public static DateTime ToDateTime(String str)
105ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
106InBlock.gif            DateTime outValue;
107InBlock.gif
108InBlock.gif            if (!String.IsNullOrEmpty(str))
109ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
110InBlock.gif                if (DateTime.TryParse(str, out outValue))
111ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
112InBlock.gif                    return outValue;
113ExpandedSubBlockEnd.gif                }

114ExpandedSubBlockEnd.gif            }

115InBlock.gif
116InBlock.gif            return DateTime.MinValue;
117ExpandedSubBlockEnd.gif        }

118InBlock.gif
119ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
120InBlock.gif        /// 将字符串转换为DateTime
121InBlock.gif        /// </summary>
122InBlock.gif        /// <param name="str"></param>
123ExpandedSubBlockEnd.gif        /// <returns></returns>

124InBlock.gif        public static DateTime ToDateTime(String str, DateTime defValue)
125ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
126InBlock.gif            DateTime outValue;
127InBlock.gif
128InBlock.gif            if (!String.IsNullOrEmpty(str))
129ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
130InBlock.gif                if (DateTime.TryParse(str, out outValue))
131ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
132InBlock.gif                    return outValue;
133ExpandedSubBlockEnd.gif                }

134ExpandedSubBlockEnd.gif            }

135InBlock.gif
136InBlock.gif            return defValue;
137ExpandedSubBlockEnd.gif        }

138InBlock.gif
139ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
140InBlock.gif        /// 将对象转换为DateTime
141InBlock.gif        /// </summary>
142InBlock.gif        /// <param name="obj">The obj.</param>
143InBlock.gif        /// <param name="defValue">The def value.</param>
144ExpandedSubBlockEnd.gif        /// <returns></returns>

145InBlock.gif        public static DateTime ToDateTime(object obj, DateTime defValue)
146ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
147InBlock.gif            if (obj != null)
148ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
149InBlock.gif                defValue = ToDateTime(obj.ToString(), defValue);
150ExpandedSubBlockEnd.gif            }

151InBlock.gif
152InBlock.gif            return defValue; 
153ExpandedSubBlockEnd.gif        }

154InBlock.gif
155ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
156InBlock.gif        /// 将字符串转换为Boolean
157InBlock.gif        /// </summary>
158InBlock.gif        /// <param name="str"></param>
159ExpandedSubBlockEnd.gif        /// <returns></returns>

160InBlock.gif        public static Boolean ToBoolean(String str)
161ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
162InBlock.gif            if (str == "0" || str.ToLower() == "false")
163ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
164InBlock.gif                return false;
165ExpandedSubBlockEnd.gif            }

166InBlock.gif            else
167ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
168InBlock.gif                return true;
169ExpandedSubBlockEnd.gif            }

170ExpandedSubBlockEnd.gif        }

171InBlock.gif
172ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
173InBlock.gif        /// 将字符串转换为Boolean
174InBlock.gif        /// </summary>
175InBlock.gif        /// <param name="str"></param>
176ExpandedSubBlockEnd.gif        /// <returns></returns>

177InBlock.gif        public static Boolean ToBoolean(Object obj)
178ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
179InBlock.gif            if (obj == null)
180ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
181InBlock.gif                return false;
182ExpandedSubBlockEnd.gif            }

183InBlock.gif            else
184ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
185InBlock.gif                return ToBoolean(obj.ToString());
186ExpandedSubBlockEnd.gif            }

187ExpandedSubBlockEnd.gif        }

188InBlock.gif
189ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
190InBlock.gif        /// 将字符串转换为Decimal
191InBlock.gif        /// </summary>
192InBlock.gif        /// <param name="str"></param>
193ExpandedSubBlockEnd.gif        /// <returns></returns>

194InBlock.gif        public static Decimal ToDecimal(String str, Decimal defValue)
195ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
196InBlock.gif            Decimal outValue;
197InBlock.gif
198InBlock.gif            if (!String.IsNullOrEmpty(str))
199ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
200InBlock.gif                if (Decimal.TryParse(str, out outValue))
201ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
202InBlock.gif                    return outValue;
203ExpandedSubBlockEnd.gif                }

204ExpandedSubBlockEnd.gif            }

205InBlock.gif
206InBlock.gif            return defValue;
207ExpandedSubBlockEnd.gif        }

208InBlock.gif
209ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
210InBlock.gif        /// 将字符串转换为Double
211InBlock.gif        /// </summary>
212InBlock.gif        /// <param name="str"></param>
213ExpandedSubBlockEnd.gif        /// <returns></returns>

214InBlock.gif        public static Double ToDouble(String str, Double defValue)
215ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
216InBlock.gif            Double outValue;
217InBlock.gif
218InBlock.gif            if (!String.IsNullOrEmpty(str))
219ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
220InBlock.gif                if (Double.TryParse(str, out outValue))
221ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
222InBlock.gif                    return outValue;
223ExpandedSubBlockEnd.gif                }

224ExpandedSubBlockEnd.gif            }

225InBlock.gif
226InBlock.gif            return defValue;
227ExpandedSubBlockEnd.gif        }

228InBlock.gif
229ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
230InBlock.gif        /// 将字符串转换为Float
231InBlock.gif        /// </summary>
232InBlock.gif        /// <param name="str"></param>
233ExpandedSubBlockEnd.gif        /// <returns></returns>

234InBlock.gif        public static Single ToFloat(String str, Single defValue)
235ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
236InBlock.gif            float outValue;
237InBlock.gif
238InBlock.gif            if (!String.IsNullOrEmpty(str))
239ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
240InBlock.gif                if (float.TryParse(str, out outValue))
241ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
242InBlock.gif                    return outValue;
243ExpandedSubBlockEnd.gif                }

244ExpandedSubBlockEnd.gif            }

245InBlock.gif
246InBlock.gif            return defValue;
247ExpandedSubBlockEnd.gif        }

248InBlock.gif
249ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
250InBlock.gif        /// 将字符串转换成Sql数据库的bit 型(true 时返回 1, 其他返回0)
251InBlock.gif        /// </summary>
252InBlock.gif        /// <param name="isTrue">if set to <c>true</c> [is true].</param>
253ExpandedSubBlockEnd.gif        /// <returns></returns>

254InBlock.gif        public static int ToDbBit(bool isTrue)
255ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
256InBlock.gif            int result = 0;
257InBlock.gif            if (isTrue)
258ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
259InBlock.gif                result = 1;
260ExpandedSubBlockEnd.gif            }

261InBlock.gif            return result;
262ExpandedSubBlockEnd.gif        }

263ExpandedSubBlockEnd.gif    }

264ExpandedBlockEnd.gif}

265 None.gif

转载于:https://www.cnblogs.com/guolichun/archive/2008/04/24/1169622.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值