-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConverterExtension.cs
107 lines (90 loc) · 2.71 KB
/
ConverterExtension.cs
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
using System;
namespace Duke.ConverterExtension
{
public static class ConverterExtension
{
public static String ToSaveString(this object obj)
{
if (obj == null)
return "";
return obj.ToString();
}
public static String ToSaveTrimString(this object obj)
{
if (obj == null)
return "";
return obj.ToString().Trim();
}
public static String ToSaveTrimUpperString(this object obj)
{
if (obj == null)
return "";
return obj.ToString().Trim().ToUpper();
}
public static int ToSaveInt(this object obj)
{
if (obj == null)
return 0;
int result = 0;
if (Int32.TryParse(obj.ToSaveString(), out result))
return result;
return result;
}
public static decimal ToSaveDecimal(this object obj)
{
if (obj == null)
return 0;
decimal result = 0;
if (decimal.TryParse(obj.ToSaveString(), out result))
return result;
return result;
}
public static string ToSaveDbString(this object obj)
{
if (obj == null)
return "";
return (obj.ToSaveString() ?? "").Replace("'", "''");
}
public static string ToSaveDbString(this object obj, int limit)
{
if (obj == null)
return "";
string temp = obj.ToSaveString().Trim();
if (limit <= 0 || temp.Length <= limit)
{
return (temp ?? "").Replace("'", "''");
}
else return temp.Substring(0, limit).Replace("'", "''");
}
public static bool ToSaveBool(this object obj)
{
bool Temp = false;
if (obj == null)
return false;
string str = obj.ToSaveString();
if (!String.IsNullOrEmpty(str))
{
bool.TryParse(str, out Temp);
}
return Temp;
}
public static double ToSaveDouble(this object obj)
{
if (obj == null)
return 0;
double result = 0;
if (double.TryParse(obj.ToSaveString(), out result))
return result;
return result;
}
public static DateTime ToSaveDateTime(this object obj)
{
DateTime retVal = DateTime.MinValue;
if (obj != null)
{
DateTime.TryParse(obj.ToSaveString(), out retVal);
}
return retVal;
}
}
}