Can I control the number of digits displayed in debugger windows for float and double variables?

后端 未结 6 1913
别跟我提以往
别跟我提以往 2021-01-08 00:51

In Visual Studio 2012, I\'m looking for a way to customize the default display of floating point types in the Autos, Locals, and Watch windows. I\'m familiar with the Native

相关标签:
6条回答
  • 2021-01-08 01:32

    You don't have much control over this. We were able to get something a little bit better than the default view, however, by casting our doubles to floats for the preview:

    <DisplayString>{{x={(float)x} y={(float)y}}}</DisplayString>

    We left them as doubles in the unfolded version so that you can see the true values. This was enough for us to pack four or five values horizontally, instead of the usual three. This still has some disadvantages: The last couple of digits on the float will be garbage, and might make you think you have precision errors when in fact you don't. However, until Microsoft gets around to adding proper formatting specifiers for numeric output, this is perhaps the best we can do.

    Incidentally, you can probably get a similar "chopped precision" by casting your floats to ints, if the values are typically large enough.

    0 讨论(0)
  • 2021-01-08 01:39

    The only way to do this that I know of is to write a native visualizer DLL. See this answer to the question "How to write a custom native visualizer DLL for Visual Studio 2012 debugger?".

    I have successfully used that, and the sources linked in that answer, to write custom visualizers that display the floating point members of various types with the precision I want.

    0 讨论(0)
  • 2021-01-08 01:42

    Unfortunately there is really no way to do this fine grained level of a change in C++ debugging. In a managed language it would be possible in some limited scenarios (when the primitives were fields of objects and annotated with special [DebuggerDisplay] attributes). For C++ though this type of customization just doesn't exist.

    0 讨论(0)
  • 2021-01-08 01:43

    Although it doesn't appear to be supported in their documentation, we have used the following definition to shorten the numbers (in VS 2015):

      <Type Name="MyVec3d">
        <DisplayString>{vectorX,g}, {vectorY,g}, {vectorZ,g}</DisplayString>
      </Type>
    
    0 讨论(0)
  • 2021-01-08 01:47

    Primitive types cannot currently be NatVizzed. However, if the specific primitives you're wanting to look at are members of another type that you can watch, you can apply formatting to that type, e.g.

    <!-- displays the double value as 0.000 -->
    <Type Name="DoubleHolder">
      <DisplayString>{(int)myDouble}.{(int)(myDouble*1000) % 1000}</DisplayString>
    </Type>
    
    0 讨论(0)
  • 2021-01-08 01:51

    Based on Overlord Zurg's answer, display the value of m_x with three digits as follows:

    <Type Name="Point">
      <DisplayString>
        {(int)m_x}.{(int)(10*m_x) % 10}{(int)(100*m_x) % 10}{(int)(1000*m_x) % 10}
      </DisplayString>
    </Type>
    

    To account for negative numbers as well:

    <Type Name="Point">
      <DisplayString>
        {(int)m_x}.{(int)((m_x&lt;0?-1:1)*10*m_x) % 10}{(int)((m_x&lt;0?-1:1)*100*m_x) % 10}{(int)((m_x&lt;0?-1:1)*1000*m_x) % 10}
      </DisplayString>
    </Type>
    
    0 讨论(0)
提交回复
热议问题