qwj
2022-08-22 63f6c5de8832eaabf066fe9f9d492187f04011b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
 
namespace CY_DocumentSynchroWCFService
{
    /// <summary>
    /// 数据类型转换类
    /// </summary>
    public class MyConvert
    {
        #region string数据转换
        public static bool GetBoolean(string value)
        {
            bool result = false;
            bool.TryParse(value, out result);
            return result;
        }
 
        public static DateTime GetDateTime(string value)
        {
            DateTime now = DateTime.Now;
            DateTime.TryParse(value, out now);
            return now;
        }
 
        public static decimal GetDecimal(string num)
        {
            decimal result = 0M;
            decimal.TryParse(num, out result);
            return result;
        }
 
        public static double GetDouble(string num)
        {
            double result = 0.0;
            double.TryParse(num, out result);
            return result;
        }
 
        public static float GetFloat(string num)
        {
            float result = 0f;
            float.TryParse(num, out result);
            return result;
        }
 
        public static short GetInt16(string num)
        {
            short result = 0;
            short.TryParse(num, out result);
            return result;
        }
 
        public static int GetInt32(string num)
        {
            int result = 0;
            int.TryParse(num, out result);
            return result;
        }
 
        public static long GetInt64(string num)
        {
            long result = 0L;
            long.TryParse(num, out result);
            return result;
        }
 
        #endregion
 
        #region object数据转换
 
 
 
        public static int? ConvertToInt(object targetValue)
        {
            int? returnValue = null;
            if (null == targetValue || targetValue == DBNull.Value)
            {
                return returnValue;
            }
 
            int result = 0;
            int.TryParse(targetValue.ToString(), out result);
            returnValue = result;
 
            return returnValue;
        }
 
        public static Int32? ConvertToInt32(object targetValue)
        {
            Int32? returnValue = null;
            if (null == targetValue || targetValue == DBNull.Value)
            {
                return returnValue;
            }
            else { }
 
            if (TypeParse.IsNumeric(targetValue))
                return Int32.Parse(targetValue.ToString());
            else
                return null;
 
        }
 
        public static Int64? ConvertToInt64(object targetValue)
        {
            Int64? returnValue = null;
            if (null == targetValue || targetValue == DBNull.Value)
            {
                return returnValue;
            }
 
            Int64 result = 0;
            Int64.TryParse(targetValue.ToString(), out result);
            returnValue = result;
 
            return returnValue;
        }
 
        public static long? ConvertToLong(object targetValue)
        {
            long? returnValue = null;
            if (null == targetValue || targetValue == DBNull.Value)
            {
                return returnValue;
            }
 
            long result = 0;
            long.TryParse(targetValue.ToString(), out result);
            returnValue = result;
 
            return returnValue;
        }
 
        public static Boolean ConvertIntToBoolean(object targetValue)
        {
            return targetValue != DBNull.Value ? ("1".Equals(targetValue.ToString()) || true.ToString().Equals(targetValue.ToString())) : false;
        }
        /// <summary>
        /// 转为Boolean
        /// 2013-5-22 10:00修改实现为:true\"true"\"TRUE"\1 均为true
        /// </summary>
        /// <param name="targetValue"></param>
        /// <returns></returns>
        public static Boolean ConvertToBoolean(object targetValue)
        {
            return targetValue != DBNull.Value ? (true.ToString().ToUpper().Equals(targetValue.ToString().ToUpper()) || "1".Equals(targetValue)) : false;
        }
 
        public static Double? ConvertToDouble(object targetValue)
        {
            Double? returnValue = null;
            if (null == targetValue || targetValue == DBNull.Value)
            {
                return returnValue;
            }
 
            Double result = 0;
            Double.TryParse(targetValue.ToString(), out result);
            returnValue = result;
 
            return returnValue;
        }
 
        public static decimal? ConvertToDecimal(object targetValue)
        {
            decimal? returnValue = null;
            if (null == targetValue || targetValue == DBNull.Value)
            {
                return returnValue;
            }
 
            decimal result = 0;
            decimal.TryParse(targetValue.ToString(), out result);
            returnValue = result;
 
            return returnValue;
        }
 
        public static DateTime? ConvertToDateTime(object targetValue)
        {
 
            if (null == targetValue || targetValue == DBNull.Value)
            {
                return null;
            }
            else { }
 
            string targetValueStr = targetValue.ToString();
 
            if (string.IsNullOrEmpty(targetValueStr)) { return null; } else { }
 
            DateTime returnValue;
            DateTime.TryParse(targetValueStr, out returnValue);
 
            return returnValue;
        }
 
 
        public static byte[] ConvertToByte(object targetValue)
        {
            return targetValue != DBNull.Value ? targetValue as byte[] : null;
        }
 
        public static Guid ConvertToGuid(object targetValue)
        {
            Guid outGuid = Guid.Empty;
            Guid.TryParse(string.Format("{0}", targetValue), out outGuid);
            return outGuid;
 
        }
        public static string ConvertToString(object targetValue)
        {
            return string.Format("{0}", targetValue);
        }
        #endregion
    }
}