changing Debug.Log() number of decimal places to display

I’m using Debug.Log() to print out Vector3s. However, it only displays a single digit to the right of the decimal.

For example,
myVector = Vector(0.2567, 0.1245, 0.8691);
Debug.Log("myVector = " + myVector);

Output:
myVector = (0.3, 0.1, 0.9)

However, when I print out each value separately, everything works fine.

For example,
myVector = Vector(0.2567, 0.1245, 0.8691);
Debug.Log("myVector[0] = " + myVector[0]);
Debug.Log("myVector[1] = " + myVector[1]);
Debug.Log("myVector[2] = " + myVector[2]);

Output:
myVector[0] = 0.2567
myVector[1] = 0.1245
myVector[2] = 0.8691

How do I change the number of decimal places Debug.Log() uses to print out? This would be akin to doing printf(“%2.4lf”, myVector[0]) in c.

you would use format or string

the result you see there on the vector3 comes from the Vector3 ToString behavior

Is there a way to change the default Vector3.ToString() behavior? I know I could write a wrapper function around any Vector3.ToString() calls but I was hoping for a single function call in Unity Initialization to do this.

That’s a very good question. As in unit tests, the test are failing because some small values in the vector, and ToString() doesn’t make any sense in this case

I know this is a very, very old post, but this is the first thing that pops up when searching for this, and there’s no clear answer. So I want to post the solution here for those that couldn’t figure this out. You can format the vector3 by calling vector.ToString("F3");
The number after the “F” represents the amount of decimal places. Here’s an example:

myVector = new Vector3(0.2567f, 0.1245f, 0.8691f);
Debug.Log(myVector.ToString("F4");

Output:
(0.2567, 0.1245, 0.8691)

Hopefully this helps!

This is actually nothing to do with Unity. The C# .NET reference for this is here:

These apply to ToString() formatting as well as String.Format() calls.

Here is more reading on the topic:

for test runner you can use

using NUnit.Framework;
using NUnit.Framework.Interfaces;
using Tests.Utils;
using UnityEngine;
using UnityEngine.TestRunner;

[assembly: TestRunCallback(typeof(MyTestRunCallback))]
namespace Tests.Utils
{
    public class MyTestRunCallback : ITestRunCallback
    {
        private const string FloatPrecisionFormat = "F5";
        
        public void RunStarted(ITest testsToRun)
        {
            TestContext.AddFormatter<Vector3>(val =>
                                                  ((Vector3) val).ToString(FloatPrecisionFormat));
            TestContext.AddFormatter<Vector2>(val =>
                                                  ((Vector2) val).ToString(FloatPrecisionFormat));
            TestContext.AddFormatter<Quaternion>(val =>
                                                     ((Quaternion) val).ToString(FloatPrecisionFormat));
        }

        public void RunFinished(ITestResult testResults)
        {
        }

        public void TestStarted(ITest test)
        {
        }

        public void TestFinished(ITestResult result)
        {
        }
    }
}
1 Like