How do i get the part of a float after the point?

What i want to do isn’t too complex, i think. I want to create a weighted luck-based rounding algorithm. For example, feeding 1.5 into it, will have a 50% chance each way, of giving 1, or 2. Feeding 1.79 into it will have a 79% chance of returning 2, and a 21% chance of returning 1. In other words, the chance of rounding up is equal to how far it is towards the next integer.

The simplest way i can think of to do this would be to grab whatever is after the point, and generate a random number in the 0…1 range, round up if lower, round down if higher.

but how do i get that part? is there a better way than converting it to string and searching it?

is there a better way to achieve my goal that i’m missing?

The modulus (remainder) operator (%) works with floats too.

So decimalPart = yourFloat % 1 would be what you need.

Definitely don’t use strings. You can just subtract the integer part.

I am assuming you are converting from Float to Int?

Value += Random.value;
int value = (int) Value;

Random.Value will add a random value 0 - 1.0 to the number, possibly increasing it to the next integer. The higher the decimal starts, the more likely that is. Casting it will then strip the decimal. (or you could also use Mathf.Floor if you want to keep it a Float)

(if you want negative numbers to round away from zero…)

if (value >0) Value += Random.value;
if (value <0) Value -= Random.value;
int rounded = (int) value;

The following should do what you want. As Eric said, no need to use strings.

	public float GetMyRandom (float number)
		float floored = Mathf.Floor (number);
		float fractionalPart = number - floored;
		float randNum = Random.Range (0, 1f);

		if (randNum >= fractionalPart) {
			return Mathf.Ceil (number);
		} else {
			return floored;

private int GodWill (float number) {
int p = (int)((number - (int)number) * 100);

    if (p < (int)Random.Range (0, 100))
        return (int)number + 1;
        return (int)number;

You can remove the mathf.Abs at the end if you want negative results still. Change the top float from positive to negative and you will get the same results.

float test = (float)12.4333;
        int test2 = 0;

        if(test < 0)
            test2 = Mathf.FloorToInt(Mathf.Abs(test));
            test = test + test2;

        if(test > 0)
            test2 = Mathf.FloorToInt(test);
            test = test - test2;


EDIT: I completely misread the question. This is the same solution as @Taziar 's:

public static int WeightedRandom (float weightAndBase) {
    return (int) (weightAndBase + Random.value);

An alternative way to solve this is if you explicitly specify your minimum value and your weight:

public static int WeightedRandom (int min, float weight) {
     return min + Random.value < weight ? 1 : 0;


Why is everyone mixing Random into this??? I would never want a simple calculation to be random, I want it to be deterministic (as much as floating point allows).

So how come the following hasn’t come up yet? Am I overlooking some hidden issue?

public static float DecimalPart(float value) {
     return Mathf.Abs(value - (int)value); // or you can leave out Mathf.Abs() if you want to keep the sign