- Home /
How to remove decimals from Vector3.Distance
How do I remove the decimals from my script so that my text mesh doesn't show something like 20.62634325 meters?
var Other : Transform;
function Start () {
}
function Update () {
transform.localRotation = Other.localRotation;
}
Answer by Loius · Oct 29, 2013 at 05:51 PM
Well there's absolutely no reference to anything in that code you posted, so not sure how that's supposed to help.
There are a couple easy ways to drop the extra digits:
displayValue:int = Mathf.RoundToInt(floatValue);
or
displayValue:int = (int)floatValue;
Answer by fafase · Oct 29, 2013 at 07:22 PM
ToString() can be overloaded:
float fl = 0.1234567f;
print(fl.ToString("0.00")); // prints 0.12
This. Ignore the other answers.
You can format also Vector3's the same way which can be very useful for debugging, especially as the default is only 1 decimal place.
> (Vector3.up*0.04f).ToString();
(0.0, 0.0, 0.0)
> (Vector3.up*0.04f).ToString("0.00");
(0.00, 0.04, 0.00)
Thank you Louis, but there's this thing with the distance, it's too much, like this:
Please don't ignore the fact that you can convert floats to integers :P
No don't ignore that fact entirely... just ignore it when you want a float as a String for display at a certain precision :)
Answer by Kiloblargh · Oct 29, 2013 at 05:51 PM
Multiply by 100, convert to an int, then convert back to a float and divide by 100, that will give you just 2 decimal places.
Your answer
Follow this Question
Related Questions
Cooldown button system 1 Answer
Help on attachment script! 0 Answers
Stop a enemy by pointing a light at it. 4 Answers