public static double ThrowIfNotPercentage(this double value) { if (!value.Between(0, 1, true)) { throw new Exception("Value is not a percentage"); } return value; }
public static string Dateify(this int input) { if (!input.Between(1, 31)) throw new ArgumentOutOfRangeException("input", "You cannot have a date before the 1st or after the 31st foooool"); if (input == 1) return "1st"; if (input == 2) return "2nd"; if (input == 3) return "3rd"; if (input == 21) return "21st"; if (input == 22) return "22nd"; if (input == 23) return "23rd"; if (input == 31) return "31st"; return string.Format("{0}th", input); }
/// <summary> /// Returns the last day of a quarter based on the date sent in /// </summary> /// <param name="Date">Date to get the last day of the quarter from</param> /// <param name="Quarter1Start">Beginning of the first quarter (defaults to the beginning of the year)</param> /// <returns>The last day of the quarter</returns> public static DateTime LastDayOfQuarter(this DateTime Date, DateTime Quarter1Start = default(DateTime)) { Date.ThrowIfNull("Date"); if (Quarter1Start.IsDefault()) Quarter1Start = Date.FirstDayOfYear(); if (Date.Between(Quarter1Start, Quarter1Start.AddMonths(3).AddDays(-1).EndOfDay())) return Quarter1Start.AddMonths(3).AddDays(-1).Date; else if (Date.Between(Quarter1Start.AddMonths(3), Quarter1Start.AddMonths(6).AddDays(-1).EndOfDay())) return Quarter1Start.AddMonths(6).AddDays(-1).Date; else if (Date.Between(Quarter1Start.AddMonths(6), Quarter1Start.AddMonths(9).AddDays(-1).EndOfDay())) return Quarter1Start.AddMonths(9).AddDays(-1).Date; return Quarter1Start.AddYears(1).AddDays(-1).Date; }
/// <summary> /// End of a specific time frame /// </summary> /// <param name="Date">Date to base off of</param> /// <param name="TimeFrame">Time frame to use</param> /// <param name="Culture">Culture to use for calculating (defaults to the current culture)</param> /// <param name="StartOfQuarter1">Start of the first quarter</param> /// <returns>The end of a specific time frame (TimeFrame.Day is the only one that sets the time to 12:59:59 PM, all else are the beginning of the day)</returns> public static DateTime EndOf(this DateTime Date, TimeFrame TimeFrame, DateTime StartOfQuarter1, CultureInfo Culture = null) { if (TimeFrame != TimeFrame.Quarter) return Date.EndOf(TimeFrame, Culture); Culture = Culture.Check(CultureInfo.CurrentCulture); if (Date.Between(StartOfQuarter1, StartOfQuarter1.AddMonths(3).AddDays(-1).EndOf(TimeFrame.Day, Culture))) return StartOfQuarter1.AddMonths(3).AddDays(-1).Date; else if (Date.Between(StartOfQuarter1.AddMonths(3), StartOfQuarter1.AddMonths(6).AddDays(-1).EndOf(TimeFrame.Day, Culture))) return StartOfQuarter1.AddMonths(6).AddDays(-1).Date; else if (Date.Between(StartOfQuarter1.AddMonths(6), StartOfQuarter1.AddMonths(9).AddDays(-1).EndOf(TimeFrame.Day, Culture))) return StartOfQuarter1.AddMonths(9).AddDays(-1).Date; return StartOfQuarter1.AddYears(1).AddDays(-1).Date; }
/// <summary> /// Returns the last day of a quarter based on the date sent in /// </summary> /// <param name="date">Date to get the last day of the quarter from</param> /// <param name="quarter1Start">Beginning of the first quarter (defaults to the beginning of the year)</param> /// <returns>The last day of the quarter</returns> public static DateTime LastDayOfQuarter(this DateTime date, DateTime quarter1Start = default(DateTime)) { Guard.NotNull(date, "date"); if (quarter1Start.IsDefault()) quarter1Start = date.FirstDayOfYear(); if (date.Between(quarter1Start, quarter1Start.AddMonths(3).AddDays(-1).EndOfDay())) return quarter1Start.AddMonths(3).AddDays(-1).Date; if (date.Between(quarter1Start.AddMonths(3), quarter1Start.AddMonths(6).AddDays(-1).EndOfDay())) return quarter1Start.AddMonths(6).AddDays(-1).Date; if (date.Between(quarter1Start.AddMonths(6), quarter1Start.AddMonths(9).AddDays(-1).EndOfDay())) return quarter1Start.AddMonths(9).AddDays(-1).Date; return quarter1Start.AddYears(1).AddDays(-1).Date; }
private static string _Between(this string cad,string left,string right,bool farleft,bool farright,bool forceifnotfound=false, IgnoreCase? ignorecase=null) { if (string.IsNullOrEmpty(cad)) return null; var c = new IgnoreCase(ignorecase); var il = 0; var ir = cad.Length-1; if (!string.IsNullOrEmpty(left)) { il = (farleft) ? cad.LastIndexOf(left,c) : cad.IndexOf(left,c); if (il >= 0) il += left.Length; else if (forceifnotfound) il = 0; } if (!string.IsNullOrEmpty(right)) { ir = (farright)? cad.LastIndexOf(right, c):cad.IndexOf(right,c); if (ir >= 0) ir -= 1; else if (forceifnotfound) ir = cad.Length - 1; } if ((il < 0) || (ir < 0)) return null; return cad.Between(il, ir); }
public static bool IsValidSqlDate(this DateTime dateTime) { return dateTime.Between(new DateTime(1753, 1, 1), new DateTime(9999, 12, 31)); }
public static bool Between(this int numberToCheck, int range) { return numberToCheck.Between(-range, range); }
/// <summary> /// Afters the specified start. /// </summary> /// <param name="value">The value.</param> /// <param name="start">The start.</param> /// <returns>System.String.</returns> public static string After(this string value, int start) { return value.Between(start + 1, int.MaxValue); }
/// <summary> /// Befores the specified end. /// </summary> /// <param name="value">The value.</param> /// <param name="end">The end.</param> /// <returns>System.String.</returns> public static string Before(this string value, int end) { return (end == 0 ? string.Empty : value.Between(0, end - 1)); }
internal static string BetweenQuotation(this string text) { return text.Between("\"", "\""); }
public static double Between(this double d, double x1, double x2) { return d.Between(new Region(x1, x2)); }
public static Vector2 Between(this Random random, Vector2 min, Vector2 max) { return new Vector2(random.Between(min.X, max.X), random.Between(min.Y, max.Y)); }
/// <summary> /// Determines if a number is close to another number. /// </summary> /// <param name="d">The number in question.</param> /// <param name="target">The target number.</param> /// <param name="giveOrTake">The range above/below the target number.</param> /// <returns>True if the number is within the range.</returns> public static bool GiveOrTake(this double d, double target, double giveOrTake) { return d.Between(target - giveOrTake, target + giveOrTake); }