Skip to content

Instantly share code, notes, and snippets.

@GhatSmith
Created September 12, 2018 17:37
Show Gist options
  • Save GhatSmith/6cd198c76a3c5a65c73bcc24a19fe404 to your computer and use it in GitHub Desktop.
Save GhatSmith/6cd198c76a3c5a65c73bcc24a19fe404 to your computer and use it in GitHub Desktop.
using UnityEngine;
namespace OddTales.Framework.Core.ClassExtension
{
public static class FloatExtension
{
/// <summary>
/// Maps value from original range to new range
/// </summary>
/// <param name="oldMin">original range min</param>
/// <param name="oldMax">original range max</param>
/// <param name="newMin">new range min</param>
/// <param name="newMax">new range max</param>
/// <param name="oldValue">value to map</param>
/// <returns>value in new range</returns>
public static float Scale(float oldMin, float oldMax, float newMin, float newMax, float oldValue)
{
float OldRange = (oldMax - oldMin);
float NewRange = (newMax - newMin);
float NewValue = (((oldValue - oldMin) * NewRange) / OldRange) + newMin;
return (NewValue);
}
/// <summary> Return the value of xPercents of maxValue </summary>
public static float GetValueFromPercent(float maxValue, float xPercents) { return ((maxValue * xPercents) / 100f); }
/// <summary> Returns how many percents of maxValue is represented by value </summary>
public static float GetPercentFromValue(float maxValue, float value) { return (maxValue == 0) ? 0 : ((value / maxValue) * 100f); }
public static float RoundToNearest(float value, float nearest) { return (nearest == 0) ? 0 : (Mathf.Round((value / nearest)) * nearest); }
public static float Abs(this float a) { return System.Math.Abs(a); }
public static float Pow(this float x, float power) { return (float)System.Math.Pow(x, power); }
public static float Round(this float a) { return (float)System.Math.Round(a); }
public static float Ceil(this float a) { return (float)System.Math.Ceiling(a); }
public static float Floor(this float a) { return (float)System.Math.Floor(a); }
public static bool IsSane(this float f) { return !(float.IsInfinity(f) || float.IsNaN(f)); }
public static string ToStringPrecisely(this float value) { return value.ToString("0.0#######"); }
public static string ToStringPrecisely(this double value) { return value.ToString("0.0#######"); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment