How to set the valid range of a float var?

If i want a float var just has 4 valid digits, how ?

Try this: (C#)

float tmp = 65.123456789f; // or tmp = Random.RandomRange(0.1f,100.0f);
		Debug.Log(tmp);
		float valid_digits =4; 
		tmp *= Mathf.Pow(10, valid_digits );
		int t = (int)tmp;
		tmp = (float) t;
		tmp /= Mathf.Pow(10, valid_digits);
		Debug.Log(tmp);

How to do it with js?

Simple :

var f = Mathf.Floor(source * 10000) / 10000;

Note I never use JS in unity, but I guess it’s something like that.

I’ve forgotten about Mathf.Floor. Smag :Your solution is much more elegant:)

How about:

var f = System.Math.Round (source, 4);

–Eric

You win :slight_smile: Didn’t know about that parameter.

I only found out about that a couple weeks ago myself, and it’s only in System.Math.Round rather than Mathf.Round. :slight_smile:

–Eri

Thank a lot to Eric5h5!