/// <summary> /// Convert the values in a Vector2 (x, y) to X decimal places (Ceillings the number) /// </summary> /// <param name="vector2">The Vector2 being converted</param> /// <param name="decimalPlace">The target decimal places</param> /// <param name="rounding">Rounding preferences</param> /// <returns></returns> public static Vector2 ConvertToDecimalPlace(Vector2 vector2, int decimalPlace = 2, RoundingPreference rounding = RoundingPreference.Standard) { return(new Vector2(ConvertToDecimalPlace(vector2.x, decimalPlace, rounding), ConvertToDecimalPlace(vector2.y, decimalPlace, rounding))); }
/// <summary> /// Convert a value (double) to X decimal places (Ceillings the number) /// </summary> /// <param name="value">The value being converted</param> /// <param name="decimalPlace">The target decimal places</param> /// <param name="rounding">Rounding preferences</param> /// <returns></returns> public static double ConvertToDecimalPlace(double value, int decimalPlace = 2, RoundingPreference rounding = RoundingPreference.Standard) { //The number of 10^X (eg. For 2 decimal places: 10^2 = 10 * 10 = 100 ) int convert = (int)Math.Pow(10, decimalPlace); //Get the value before decimal point (Integer) int temp1 = (int)value; //Get the value after decimal point double temp2 = value - temp1; //Get the remaining value (eg. 12.34567 - 12 = 0.34567) temp2 = temp2 * convert; //Multiplying the value by 10^X (eg. 0.34567 * 100) //Round the value to an 'integer' switch (rounding) { //Standard Rounding (Add 1 to temp2 if the remain value >= 0.5) case RoundingPreference.Standard: temp2 = Math.Round(temp2); break; //Round up (Add 1 to temp2 and ignores the remaining value) case RoundingPreference.RoundUp: temp2 = Math.Ceiling(temp2); //Ceiling it to an integer (34.567 => 35) break; //Round down (Ignores the remaining value) case RoundingPreference.RoundDown: temp2 = Math.Floor(temp2); //Flooring it to an integer (34.567 => 34) break; } temp2 = temp2 / convert; //Get the value as an integer, and devided by 100 to convert it to 2 digits number (eg. 35/100 = 0.35) //Adding up 2 values (before and after) to get the price in 2 decimal return(temp1 + temp2); }
/// <summary> /// Convert the values in a range (min, max) to X decimal places (Ceillings the number) /// </summary> /// <param name="range">The range being converted</param> /// <param name="decimalPlace">The target decimal places</param> /// <param name="rounding">Rounding preferences</param> /// <returns></returns> public static Range ConvertToDecimalPlace(Range range, int decimalPlace = 2, RoundingPreference rounding = RoundingPreference.Standard) { return(new Range(ConvertToDecimalPlace(range.min, decimalPlace, rounding), ConvertToDecimalPlace(range.max, decimalPlace, rounding), range.rangeType)); }