ConvertTo.cs 强制转换类
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
namespace ChinaValue.CommonV2008
6
{
7
/// <summary>
8
/// 将字符串转换成任意其他数据
9
/// </summary>
10
public static class ConvertStr
11
{
12
/// <summary>
13
/// 将字符串转换为Int16
14
/// </summary>
15
/// <param name="str"></param>
16
/// <returns></returns>
17
public static Int16 ToInt16(String str, Int16 defValue)
18
{
19
Int16 outValue;
20
21
if (!String.IsNullOrEmpty(str))
22
{
23
if (Int16.TryParse(str, out outValue))
24
{
25
return outValue;
26
}
27
}
28
29
return defValue;
30
}
31
32
/// <summary>
33
/// 将字符串转换为Int32
34
/// </summary>
35
/// <param name="str"></param>
36
/// <returns></returns>
37
public static Int32 ToInt32(String str)
38
{
39
return ToInt32(str, 0);
40
}
41
42
/// <summary>
43
/// 将类型转换为Int32
44
/// </summary>
45
/// <param name="obj">The obj.</param>
46
/// <returns></returns>
47
public static Int32 ToInt32(object obj, Int32 defValue)
48
{
49
if (obj != null && obj != DBNull.Value)
50
{
51
return ToInt32(obj.ToString(), defValue);
52
}
53
else
54
{
55
return defValue;
56
}
57
}
58
59
/// <summary>
60
/// 将字符串转换为Int32
61
/// </summary>
62
/// <param name="str"></param>
63
/// <returns></returns>
64
public static Int32 ToInt32(String str, Int32 defValue)
65
{
66
Int32 outValue;
67
68
if (!String.IsNullOrEmpty(str))
69
{
70
if (Int32.TryParse(str, out outValue))
71
{
72
return outValue;
73
}
74
}
75
76
return defValue;
77
}
78
79
/// <summary>
80
/// 将字符串转换为Int64
81
/// </summary>
82
/// <param name="str"></param>
83
/// <returns></returns>
84
public static Int64 ToInt64(String str, Int64 defValue)
85
{
86
Int64 outValue;
87
88
if (!String.IsNullOrEmpty(str))
89
{
90
if (Int64.TryParse(str, out outValue))
91
{
92
return outValue;
93
}
94
}
95
96
return defValue;
97
}
98
99
/// <summary>
100
/// 将字符串转换为DateTime
101
/// </summary>
102
/// <param name="str"></param>
103
/// <returns></returns>
104
public static DateTime ToDateTime(String str)
105
{
106
DateTime outValue;
107
108
if (!String.IsNullOrEmpty(str))
109
{
110
if (DateTime.TryParse(str, out outValue))
111
{
112
return outValue;
113
}
114
}
115
116
return DateTime.MinValue;
117
}
118
119
/// <summary>
120
/// 将字符串转换为DateTime
121
/// </summary>
122
/// <param name="str"></param>
123
/// <returns></returns>
124
public static DateTime ToDateTime(String str, DateTime defValue)
125
{
126
DateTime outValue;
127
128
if (!String.IsNullOrEmpty(str))
129
{
130
if (DateTime.TryParse(str, out outValue))
131
{
132
return outValue;
133
}
134
}
135
136
return defValue;
137
}
138
139
/// <summary>
140
/// 将对象转换为DateTime
141
/// </summary>
142
/// <param name="obj">The obj.</param>
143
/// <param name="defValue">The def value.</param>
144
/// <returns></returns>
145
public static DateTime ToDateTime(object obj, DateTime defValue)
146
{
147
if (obj != null)
148
{
149
defValue = ToDateTime(obj.ToString(), defValue);
150
}
151
152
return defValue;
153
}
154
155
/// <summary>
156
/// 将字符串转换为Boolean
157
/// </summary>
158
/// <param name="str"></param>
159
/// <returns></returns>
160
public static Boolean ToBoolean(String str)
161
{
162
if (str == "0" || str.ToLower() == "false")
163
{
164
return false;
165
}
166
else
167
{
168
return true;
169
}
170
}
171
172
/// <summary>
173
/// 将字符串转换为Boolean
174
/// </summary>
175
/// <param name="str"></param>
176
/// <returns></returns>
177
public static Boolean ToBoolean(Object obj)
178
{
179
if (obj == null)
180
{
181
return false;
182
}
183
else
184
{
185
return ToBoolean(obj.ToString());
186
}
187
}
188
189
/// <summary>
190
/// 将字符串转换为Decimal
191
/// </summary>
192
/// <param name="str"></param>
193
/// <returns></returns>
194
public static Decimal ToDecimal(String str, Decimal defValue)
195
{
196
Decimal outValue;
197
198
if (!String.IsNullOrEmpty(str))
199
{
200
if (Decimal.TryParse(str, out outValue))
201
{
202
return outValue;
203
}
204
}
205
206
return defValue;
207
}
208
209
/// <summary>
210
/// 将字符串转换为Double
211
/// </summary>
212
/// <param name="str"></param>
213
/// <returns></returns>
214
public static Double ToDouble(String str, Double defValue)
215
{
216
Double outValue;
217
218
if (!String.IsNullOrEmpty(str))
219
{
220
if (Double.TryParse(str, out outValue))
221
{
222
return outValue;
223
}
224
}
225
226
return defValue;
227
}
228
229
/// <summary>
230
/// 将字符串转换为Float
231
/// </summary>
232
/// <param name="str"></param>
233
/// <returns></returns>
234
public static Single ToFloat(String str, Single defValue)
235
{
236
float outValue;
237
238
if (!String.IsNullOrEmpty(str))
239
{
240
if (float.TryParse(str, out outValue))
241
{
242
return outValue;
243
}
244
}
245
246
return defValue;
247
}
248
249
/// <summary>
250
/// 将字符串转换成Sql数据库的bit 型(true 时返回 1, 其他返回0)
251
/// </summary>
252
/// <param name="isTrue">if set to <c>true</c> [is true].</param>
253
/// <returns></returns>
254
public static int ToDbBit(bool isTrue)
255
{
256
int result = 0;
257
if (isTrue)
258
{
259
result = 1;
260
}
261
return result;
262
}
263
}
264
}
265
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace ChinaValue.CommonV20086
{7
/// <summary>8
/// 将字符串转换成任意其他数据9
/// </summary>10
public static class ConvertStr11
{12
/// <summary>13
/// 将字符串转换为Int1614
/// </summary>15
/// <param name="str"></param>16
/// <returns></returns>17
public static Int16 ToInt16(String str, Int16 defValue)18
{19
Int16 outValue;20

21
if (!String.IsNullOrEmpty(str))22
{23
if (Int16.TryParse(str, out outValue))24
{25
return outValue;26
}27
}28

29
return defValue;30
}31
32
/// <summary>33
/// 将字符串转换为Int3234
/// </summary>35
/// <param name="str"></param>36
/// <returns></returns>37
public static Int32 ToInt32(String str)38
{39
return ToInt32(str, 0);40
}41

42
/// <summary>43
/// 将类型转换为Int3244
/// </summary>45
/// <param name="obj">The obj.</param>46
/// <returns></returns>47
public static Int32 ToInt32(object obj, Int32 defValue)48
{49
if (obj != null && obj != DBNull.Value)50
{51
return ToInt32(obj.ToString(), defValue);52
}53
else54
{55
return defValue;56
}57
}58

59
/// <summary>60
/// 将字符串转换为Int3261
/// </summary>62
/// <param name="str"></param>63
/// <returns></returns>64
public static Int32 ToInt32(String str, Int32 defValue)65
{66
Int32 outValue;67

68
if (!String.IsNullOrEmpty(str))69
{70
if (Int32.TryParse(str, out outValue))71
{72
return outValue;73
}74
}75

76
return defValue;77
}78

79
/// <summary>80
/// 将字符串转换为Int6481
/// </summary>82
/// <param name="str"></param>83
/// <returns></returns>84
public static Int64 ToInt64(String str, Int64 defValue)85
{86
Int64 outValue;87

88
if (!String.IsNullOrEmpty(str))89
{90
if (Int64.TryParse(str, out outValue))91
{92
return outValue;93
}94
}95

96
return defValue;97
}98

99
/// <summary>100
/// 将字符串转换为DateTime101
/// </summary>102
/// <param name="str"></param>103
/// <returns></returns>104
public static DateTime ToDateTime(String str)105
{106
DateTime outValue;107

108
if (!String.IsNullOrEmpty(str))109
{110
if (DateTime.TryParse(str, out outValue))111
{112
return outValue;113
}114
}115

116
return DateTime.MinValue;117
}118

119
/// <summary>120
/// 将字符串转换为DateTime121
/// </summary>122
/// <param name="str"></param>123
/// <returns></returns>124
public static DateTime ToDateTime(String str, DateTime defValue)125
{126
DateTime outValue;127

128
if (!String.IsNullOrEmpty(str))129
{130
if (DateTime.TryParse(str, out outValue))131
{132
return outValue;133
}134
}135

136
return defValue;137
}138

139
/// <summary>140
/// 将对象转换为DateTime141
/// </summary>142
/// <param name="obj">The obj.</param>143
/// <param name="defValue">The def value.</param>144
/// <returns></returns>145
public static DateTime ToDateTime(object obj, DateTime defValue)146
{147
if (obj != null)148
{149
defValue = ToDateTime(obj.ToString(), defValue);150
}151

152
return defValue; 153
}154

155
/// <summary>156
/// 将字符串转换为Boolean157
/// </summary>158
/// <param name="str"></param>159
/// <returns></returns>160
public static Boolean ToBoolean(String str)161
{162
if (str == "0" || str.ToLower() == "false")163
{164
return false;165
}166
else167
{168
return true;169
}170
}171

172
/// <summary>173
/// 将字符串转换为Boolean174
/// </summary>175
/// <param name="str"></param>176
/// <returns></returns>177
public static Boolean ToBoolean(Object obj)178
{179
if (obj == null)180
{181
return false;182
}183
else184
{185
return ToBoolean(obj.ToString());186
}187
}188

189
/// <summary>190
/// 将字符串转换为Decimal191
/// </summary>192
/// <param name="str"></param>193
/// <returns></returns>194
public static Decimal ToDecimal(String str, Decimal defValue)195
{196
Decimal outValue;197

198
if (!String.IsNullOrEmpty(str))199
{200
if (Decimal.TryParse(str, out outValue))201
{202
return outValue;203
}204
}205

206
return defValue;207
}208

209
/// <summary>210
/// 将字符串转换为Double211
/// </summary>212
/// <param name="str"></param>213
/// <returns></returns>214
public static Double ToDouble(String str, Double defValue)215
{216
Double outValue;217

218
if (!String.IsNullOrEmpty(str))219
{220
if (Double.TryParse(str, out outValue))221
{222
return outValue;223
}224
}225

226
return defValue;227
}228

229
/// <summary>230
/// 将字符串转换为Float231
/// </summary>232
/// <param name="str"></param>233
/// <returns></returns>234
public static Single ToFloat(String str, Single defValue)235
{236
float outValue;237

238
if (!String.IsNullOrEmpty(str))239
{240
if (float.TryParse(str, out outValue))241
{242
return outValue;243
}244
}245

246
return defValue;247
}248

249
/// <summary>250
/// 将字符串转换成Sql数据库的bit 型(true 时返回 1, 其他返回0)251
/// </summary>252
/// <param name="isTrue">if set to <c>true</c> [is true].</param>253
/// <returns></returns>254
public static int ToDbBit(bool isTrue)255
{256
int result = 0;257
if (isTrue)258
{259
result = 1;260
}261
return result;262
}263
}264
}265

Ajax.jQuery.Java.



浙公网安备 33010602011771号