ShareX/ShareX.HelpersLib/Extensions/NumberExtensions.cs

116 lines
4.1 KiB
C#
Raw Normal View History

2014-07-04 17:38:04 +12:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2014-07-04 17:38:04 +12:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
2014-07-04 17:38:04 +12:00
{
public static class NumberExtensions
{
2019-05-24 08:45:11 +12:00
private static readonly string[] suffixDecimal = new[] { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
private static readonly string[] suffixBinary = new[] { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" };
public static T Min<T>(this T num, T min) where T : IComparable<T>
2014-07-04 17:38:04 +12:00
{
2019-05-24 08:45:11 +12:00
return MathHelpers.Min(num, min);
2014-07-04 17:38:04 +12:00
}
2019-05-24 08:45:11 +12:00
public static T Max<T>(this T num, T max) where T : IComparable<T>
2014-07-04 17:38:04 +12:00
{
2019-05-24 08:45:11 +12:00
return MathHelpers.Max(num, max);
2014-07-04 17:38:04 +12:00
}
2019-05-24 07:59:16 +12:00
public static T Clamp<T>(this T num, T min, T max) where T : IComparable<T>
2014-07-04 17:38:04 +12:00
{
2019-05-24 07:59:16 +12:00
return MathHelpers.Clamp(num, min, max);
}
2019-05-24 08:45:11 +12:00
public static bool IsBetween<T>(this T num, T min, T max) where T : IComparable<T>
2014-07-04 17:38:04 +12:00
{
2019-05-24 08:45:11 +12:00
return MathHelpers.IsBetween(num, min, max);
2014-07-04 17:38:04 +12:00
}
2021-06-10 10:14:01 +12:00
public static T BetweenOrDefault<T>(this T num, T min, T max, T defaultValue = default) where T : IComparable<T>
2014-07-04 17:38:04 +12:00
{
2019-05-24 08:45:11 +12:00
return MathHelpers.BetweenOrDefault(num, min, max, defaultValue);
2014-07-04 17:38:04 +12:00
}
2019-05-24 08:45:11 +12:00
public static float Remap(this float value, float from1, float to1, float from2, float to2)
2014-07-04 17:38:04 +12:00
{
2019-05-24 08:45:11 +12:00
return MathHelpers.Remap(value, from1, to1, from2, to2);
2014-07-04 17:38:04 +12:00
}
2019-05-24 08:45:11 +12:00
public static bool IsEvenNumber(this int num)
2014-07-04 17:38:04 +12:00
{
2019-05-24 08:45:11 +12:00
return MathHelpers.IsEvenNumber(num);
2014-07-04 17:38:04 +12:00
}
2019-05-24 08:45:11 +12:00
public static bool IsOddNumber(this int num)
2014-07-04 17:38:04 +12:00
{
2019-05-24 08:45:11 +12:00
return MathHelpers.IsOddNumber(num);
2014-07-04 17:38:04 +12:00
}
public static string ToSizeString(this long size, bool binary = false, int decimalPlaces = 2)
{
int bytes = binary ? 1024 : 1000;
if (size < bytes) return Math.Max(size, 0) + " B";
int place = (int)Math.Floor(Math.Log(size, bytes));
double num = size / Math.Pow(bytes, place);
2019-05-24 08:45:11 +12:00
string suffix = binary ? suffixBinary[place] : suffixDecimal[place];
return num.ToDecimalString(decimalPlaces.Clamp(0, 3)) + " " + suffix;
2014-07-04 17:38:04 +12:00
}
public static string ToDecimalString(this double number, int decimalPlaces)
{
string format = "0";
if (decimalPlaces > 0) format += "." + new string('0', decimalPlaces);
return number.ToString(format);
}
public static string ToBase(this int value, int radix, string digits)
{
if (string.IsNullOrEmpty(digits))
{
throw new ArgumentNullException("digits", string.Format("Digits must contain character value representations"));
}
radix = Math.Abs(radix);
if (radix > digits.Length || radix < 2)
{
throw new ArgumentOutOfRangeException("radix", radix, string.Format("Radix has to be > 2 and < {0}", digits.Length));
}
2016-05-25 06:15:45 +12:00
string result = "";
int quotient = Math.Abs(value);
2018-05-16 20:23:13 +12:00
while (quotient > 0)
{
int temp = quotient % radix;
result = digits[temp] + result;
quotient /= radix;
}
return result;
}
2014-07-04 17:38:04 +12:00
}
}