Reputation: 437
If I have a list of vector3's I noticed that sometimes the values will get rounded, but i'm not sure why.
I am using UnityEngine.Vector3.
List<Vector3> verticiesList = new List<Vector3> { };
verticiesList.Add(new Vector3(0.5f, 0.65f, 0));
Debug.Log(verticiesList[0]);
The result of the log is 0.5,0.7,0.0
how can i give the correct values to mesh.vertices
?
I would like to understand why this is happening.
Upvotes: 1
Views: 1311
Reputation: 90724
The answer is yes! Unity rounds them to be better readable.
From the Vector3
source code
public override string ToString() { return ToString(null, CultureInfo.InvariantCulture.NumberFormat); } public string ToString(string format) { return ToString(format, CultureInfo.InvariantCulture.NumberFormat); } public string ToString(string format, IFormatProvider formatProvider) { if (string.IsNullOrEmpty(format)) format = "F1"; return UnityString.Format("({0}, {1}, {2})", x.ToString(format, formatProvider), y.ToString(format, formatProvider), z.ToString(format, formatProvider)); }
The default Fixed-point format specifier (F1
) rounds the output to 1
fixed digit.
if you want the exact values use e.g.
Debug.Log(verticiesList[0].ToString("G"));
the General format specifier (G
) prints up to 7 digits for float
values.
If your goal was to store and later restore values e.g. when writing the values to a file etc then rather use G9
instead
When used with a Single value, the "G9" format specifier ensures that the original Single value successfully round-trips.
Already needed this once for a since project.
Upvotes: 4
Reputation: 853
Using the ToString Method Parameter you can print the floats to what ever decimal place you want:
Debug.Log(verticalList[0].ToString("F6")); //will show 6 decimal places
Upvotes: 4