Utilities.cs

v4.0

Rounding is an enum with options None, Floor, Ceiling, and Round.

RoundThis()

Send in a float value, and a number of decimal places, and optionally a different rounding method, and get a new value back!

public static float RoundThis(float value
    , int decimals, Rounding roundingOption = Rounding.Round)

RoundToDecimal()

RoundThis() may actually call this method, depending on the options. This takes in a float value and int decimals, and returns the float rounded to the number of decimals.

public static float RoundToDecimal(float value, int decimals = 2)

RandomFloat()

Returns a float value between min and max. If none are provided, will return between 0f and 1f.

public static float RandomFloat(float min = 0f, float max = 1f)

// Note max is exclusive, meaning it will never return exactly that value. However, 
// mixing methods together can solve this!

// This example will create a random value, and round it to two decimal places. If
// the result of RandomFloat() is 0.9962, then it will be rounded to 1f.
var randomValue => Utilities.RoundToDecimal(RandomFloat());

RandomFloatBestOf()

This will return a value between min and max as well. However, it will generate multiple numbers, and return the highest or lowest, depending on the value of higherIsBetter. Defaults to higher values being better, 2 rolls, and 0f to 1f.

public static float RandomFloatBestOf(bool higherIsBetter = true
    , int rolls = 2, float min = 0f, float max = 1f)

RandomInt()

Returns an int value between min and max. Defaults to 0 to 100.

public static int RandomInt(int min = 0, int max = 100)

RandomInt() can return the highest value. Random.Range is inclusive when int values are being used.

Last updated