- Home /
Set Precision of Scientific Notation
What I mean is, in the inspector, it will say something like "2.56739276596e74" or something, how could i simplify this to just, like, 2.57e74? Is there a simple command you can give like for removing scientific notation ("n0")? or does it require some coding magic that will take a while?
Answer by Eno-Khaon · Feb 01, 2016 at 11:14 PM
According to Microsoft's page on Standard Numeric Format Strings, you should be able to specify the apparent accuracy of scientific notation using the "G" modifier.
In this case, "G2" should give you only two digits of accuracy to match your example.
how simple and elegant. Thanks. I tested it out and it works perfectly.
Answer by Bunny83 · Feb 02, 2016 at 12:11 AM
Well, unfortunately there's nothing build into the Editor that would allow you to change the default behaviour. The editor uses internal static string fields which contains a format string which is used by FloatField and "DoubleField".
// inside UnityEditor.EditorGUI
internal static string kFloatFieldFormatString = "g7";
internal static string kDoubleFieldFormatString = "g15";
The problem is, when you would change those default format strings (using reflection or something like that) it would affect not only how the values are displayed. Since those are edit-fields the actual value would be restricted to the given range when you edit the field.
You can however customize how a field is displayed / treated by implementing either a custom inspector for your class, or a PropertyDrawer to customize how a single field is handled. The numberfields (FloatField and DoubleField) are quite complicated and as said they don't allow to pass a custom format string. You can either replicate them yourself, or create a simple wrapper around them which uses reflection to change those internal format strings temporarily.
If you want to know how the FloatField / DoubleField is implemented, you can use "ILSpy" or any other .NET / Mono reflector and open the UnityEditor.dll.
that was very informative about how the engine works, I appreciate the information.