-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIntegerExtensions.cs
More file actions
286 lines (254 loc) · 9.12 KB
/
IntegerExtensions.cs
File metadata and controls
286 lines (254 loc) · 9.12 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.Globalization;
namespace Extensions
{
public static class NumberExtensions
{
/// <summary>
/// Ensures that the specified integer is positive
/// </summary>
/// <param name="number"> The integer we are ensuring is positive. </param>
/// <returns> Positive Integer. </returns>
public static int EnsurePositive(this int number)
{
return (number < 0 ? (number*-1) : number);
}
/// <summary>
/// Ensures that the specified float is positive
/// </summary>
/// <param name="number"> The float we are ensuring is positive </param>
/// <returns> </returns>
public static float EnsurePositive(this float number)
{
return (number < 0 ? (number*-1F) : number);
}
/// <summary>
/// Convert integer to string with a minimum length padding with "0" if int is to short.
/// </summary>
/// <param name="number"> The number we are coverting to a fixed string length. </param>
/// <param name="length"> The length to fix the integer to. </param>
/// <returns> The new fixed length string </returns>
public static string ForceLength(this int number, int length)
{
string s = number.ToString(CultureInfo.InvariantCulture);
string returnString = "";
for (int i = s.Length; i < length; i++)
{
returnString += "0";
}
return returnString + s;
}
/// <summary>
/// Returns true if number is even
/// </summary>
/// <param name="value"> The value to check. </param>
/// <returns> <c>true</c> If number is even. <c>If numbner is odd.</c> </returns>
public static bool IsEven(this int value)
{
return value%2 == 0;
}
/// <summary>
/// Returns true if number is odd
/// </summary>
/// <param name="value"> </param>
/// <returns> <c>true</c> If number is odd. <c>If numbner is even.</c> </returns>
public static bool IsOdd(this int value)
{
return value%2 != 0;
}
/// <summary>
/// Returns a binary string representation of the number.
/// </summary>
/// <param name="number"> The integer number to convert. </param>
/// <returns> A binary string of the number provided. </returns>
public static string ToBinary(this int number)
{
return Convert.ToString(number, 2);
}
/// <summary>
/// Returns a hexadecimal string representation of the number.
/// </summary>
/// <param name="number"> The integer number to convert. </param>
/// <returns> A hex string of the number provided </returns>
public static string ToHex(this int number)
{
return Convert.ToString(number, 16);
}
/// <summary>
/// Convert an nibble value(4 bit) integer to its corresponding hex value
/// </summary>
/// <param name="nibble"> </param>
/// <returns> </returns>
public static int ToHexChar(this int nibble)
{
if (nibble < 10)
return nibble + 48;
return nibble + 55;
}
/// <summary>
/// Returns a string representation using the Current culture
/// </summary>
/// <param name="instance"> </param>
/// <returns> </returns>
public static string ToStringCurrentCulture(this int instance)
{
return instance.ToString(CultureInfo.CurrentCulture);
}
/// <summary>
/// Returns a string representation using an invariant culture
/// </summary>
/// <param name="instance"> </param>
/// <returns> </returns>
public static string ToStringInvariantCulture(this int instance)
{
return instance.ToString(CultureInfo.InvariantCulture);
}
#region DateTime
/// <summary>
/// Returns a date in the past by given number of days.
/// </summary>
/// <param name="days"> The days. </param>
/// <returns> </returns>
public static DateTime DaysAgo(this int days)
{
var t = new TimeSpan(days, 0, 0, 0);
return DateTime.Now.Subtract(t);
}
/// <summary>
/// Returns a date in the future by days.
/// </summary>
/// <param name="days"> The days. </param>
/// <returns> </returns>
public static DateTime DaysFromNow(this int days)
{
var t = new TimeSpan(days, 0, 0, 0);
return DateTime.Now.Add(t);
}
/// <summary>
/// Returns a date in the past by hours.
/// </summary>
/// <param name="hours"> The hours. </param>
/// <returns> </returns>
public static DateTime HoursAgo(this int hours)
{
var t = new TimeSpan(hours, 0, 0);
return DateTime.Now.Subtract(t);
}
/// <summary>
/// Returns a date in the future by hours.
/// </summary>
/// <param name="hours"> The hours. </param>
/// <returns> </returns>
public static DateTime HoursFromNow(this int hours)
{
var t = new TimeSpan(hours, 0, 0);
return DateTime.Now.Add(t);
}
/// <summary>
/// Returns a date in the past by minutes
/// </summary>
/// <param name="minutes"> The minutes. </param>
/// <returns> </returns>
public static DateTime MinutesAgo(this int minutes)
{
var t = new TimeSpan(0, minutes, 0);
return DateTime.Now.Subtract(t);
}
/// <summary>
/// Returns a date in the future by minutes.
/// </summary>
/// <param name="minutes"> The minutes. </param>
/// <returns> </returns>
public static DateTime MinutesFromNow(this int minutes)
{
var t = new TimeSpan(0, minutes, 0);
return DateTime.Now.Add(t);
}
/// <summary>
/// Gets a date in the past according to seconds
/// </summary>
/// <param name="seconds"> The seconds. </param>
/// <returns> </returns>
public static DateTime SecondsAgo(this int seconds)
{
var t = new TimeSpan(0, 0, seconds);
return DateTime.Now.Subtract(t);
}
/// <summary>
/// Gets a date in the future by seconds.
/// </summary>
/// <param name="seconds"> The seconds. </param>
/// <returns> </returns>
public static DateTime SecondsFromNow(this int seconds)
{
var t = new TimeSpan(0, 0, seconds);
return DateTime.Now.Add(t);
}
#endregion
#region TimeSpan
/// <summary>
/// Converts the number to days as a TimeSpan.
/// </summary>
/// <param name="num"> Number representing days </param>
/// <returns> </returns>
public static TimeSpan Days(this int num)
{
return new TimeSpan(num, 0, 0, 0);
}
/// <summary>
/// Converts the number to hours as a TimeSpan
/// </summary>
/// <param name="num"> Number representing hours </param>
/// <returns> </returns>
public static TimeSpan Hours(this int num)
{
return new TimeSpan(0, num, 0, 0);
}
/// <summary>
/// Converts the number of minutes as a TimeSpan
/// </summary>
/// <param name="num"> Number representing minutes </param>
/// <returns> </returns>
public static TimeSpan Minutes(this int num)
{
return new TimeSpan(0, 0, num, 0);
}
/// <summary>
/// Converts the number to seconds as a TimeSpan
/// </summary>
/// <param name="num"> </param>
/// <returns> </returns>
public static TimeSpan Seconds(this int num)
{
return new TimeSpan(0, 0, 0, num);
}
/// <summary>
/// Converts the number to a TimeSpan.
/// </summary>
/// <param name="num"> Number reprsenting a timespan </param>
/// <returns> </returns>
public static TimeSpan Time(this int num)
{
return Time(num, false);
}
/// <summary>
/// Converts the military time to a timespan.
/// </summary>
/// <param name="num"> </param>
/// <param name="convertSingleDigitsToHours"> Indicates whether to treat "9" as 9 hours instead of minutes. </param>
/// <returns> </returns>
public static TimeSpan Time(this int num, bool convertSingleDigitsToHours)
{
if (convertSingleDigitsToHours)
{
if (num <= 24)
num *= 100;
}
int hours = num/100;
int minutes = num%100;
var time = new TimeSpan(hours, minutes, 0);
return time;
}
#endregion
}
}