- Home /
print to only two decimal places.
Hello,
Problem:
When I do a division with the resultant value in float, I get values more than 3-4 decimal places. I would have to print this on the console.
What should I do in order to print it to just 2 decimal places.
Code:
Debug.Log("Distance travelled" + distance + " m");
The NEED :
Considering the above code, the value distance prints itself to 4 decimals places. i.e. I would like to have it printed as 4.56 rather than 4.56234.
THANK YOU VERY MUCH IN ADVANCE
@$$anonymous$$eryu
Add that as an answer ins$$anonymous$$d of a comment and I'll vote it up for you. You're absolutely right - this isn't a Unity question, it's a basic javascript question.
oh, and here are the equivalent in JS:
@NOAA_Julien: Unity doesn't use web Javascript, so that's not the "equivalent" and won't work in Unity. The Unityscript equivalent of "`decimalVar.ToString ("#.##");`" is--wait for it--"`decimalVar.ToString ("#.##");`".
I certainly do stand corrected. I program exclusively in C#, so JS and US are basically the same thing as far as I'm concerned :).
Not sure why the fact that you don't use it would make it similar to javascript.
UnityScript is a translation of C# that uses a syntax similar to javascript.
Answer by aldonaletto · Dec 07, 2011 at 09:07 PM
You can use this:
Debug.Log("Distance travelled" + distance.ToString("F2") + " m");
"Fn" format the float value to n decimal places.
Answer by Keryu · Dec 07, 2011 at 08:22 PM
Any of these solutions should work nicely for you: http://stackoverflow.com/questions/164926/c-sharp-how-do-i-round-a-decimal-value-to-2-decimal-places-for-output-on-a-pa
Your answer
![](https://koobas.hobune.stream/wayback/20220613061857im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Resticing variables to whole numbers 3 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
I need help with a script(brauche hilfe mit einen Script) 0 Answers