Float Precision in Unity Javascript

I’m wondering, how do I set the float precision in Unity Javascript?

That is, if I want a given float to truncate or round to a specific number of decimal places, how would I do that?

TIA!

Mathf.Round, Mathf.Floor, Mathf.Ceil

But don’t those all round to integers?

Here is one way to do what you want.

var num : float = 1.23456

num = Mathf.Floor

Here is one way to do what you want.

var num : float = 1.23456

num = Mathf.Floor

var decimal = 0.58932986;
var roundedSome = Mathf.Round(decimal * 1000) / 1000;

:stuck_out_tongue:

my post keeps getting cut off for some reason

This was the gist of it anyway.

var num : float = 1.23456
num = Mathf.Round(num * 10) / 10;

Thanks, Yoggy.

I thought about trying what you suggest, but figured there might be an (undocumented) Mathf function that would do the job.

Guess not, huh?

I wonder why Unity JS doesn’t implement toFixed() or toPrecision()?

or more precisely, if you just want to truncate:

var num : float = 1.23456
num = Mathf.Floor(num * 10) / 10;

Thanks to you too, Joe!

Glad to see you were finally able to tame that browser. :wink:

Maybe I need a little more clarification, but this is not working for me, or at least it is not printing out on the console like I believe it should.

Here is my simple example, and the results:

var distPrecision : int = 100;
var numOne : float = 100.12345;
var numTwo : float = 100.12345;

function RoundToDecimal() {
     numOne = Mathf.Floor((numOne * distPrecision) / distPrecision);
     numTwo = Mathf.Floor((numTwo * distPrecision) / distPrecision);

     print(numOne + " " + numTwo);
}

And the results in the console are 100 100, so why is it not 100.12 and 100.12?

Thanks

Edit: Figured it out, I was including the division of the number in the Mathf.Floor instead of just the multiplication. Phew, thought I was losing my mind :slight_smile:

-Raiden

Actually, Mathf.Round(value*100)/100 is not identical to the toFixed(2) function - the Mathf way will print 10.0 as “10” while the toFixed() way will print it as “10.00”

I found a solution on the web and converted it to Unity’s slightly-different-than-browsers-do-it Javascript:

function ShowNumber(number:float, decimalPlaces:int) {
    var factor = Mathf.Pow(10, decimalPlaces || 0);
    var v = (Mathf.Round(Mathf.Round(number * factor * 100) / 100) / factor).ToString();
    if (v.IndexOf('.') >= 0) {
        return v + factor.ToString().Substring(v.length - v.IndexOf('.'));
    }
    return v + '.' + factor.ToString().Substring(1);
}

What I usually do is something like, this is a shortened version, but the idea is there. f[0-9] determines how many decimals will stay, no rounding.

var theFloat:float = 1.23456789;
var double:float = float.Parse(theFloat.ToString(“f2”));

Thanks for posting this stuff just used it :slight_smile:

Thank You!!!