Reputation: 13
I need to round a float to -1 or 1. If the float equals 0 then leave it at zero. Right now I use this:
Mathf.Clamp(x*1000000, -1f,1)
is there a more elegant solution?
Upvotes: 1
Views: 437
Reputation: 27609
You could use Math.Sign(x)
to do this. It will return 0 when x=0, -1 if x is negative and +1 if x is positive. I believe this does what you want. Documentation here: https://msdn.microsoft.com/en-us/library/z394hhsx(v=vs.110).aspx
Upvotes: 5