简体   繁体   中英

How to display 1000 separator in C# Visual Studio debugger

When working with big numbers, especially long , it's a must to display them with 1000 separators.

Is there a way to display those separators?

Inside the watch window?

观察窗口示例

When hovering over a variable?

徘徊的例子

it's technically possible but not straightforward. Your easiest way would be to create a 2nd formatted variable and watch that instead.

The only way i know of to do this in the watch window is you can actually "watch" an expression by typing it in the watch window as an edit to the variable. The expression would append a formatted toString. This doesn't seem to work with arrays though.

Say however you had a variable "total". your watch would say total and you would double click on it and edit it to say total.ToString("C");

I hope this helps.

I would suggest you to create a new array(stringNumbers), which is having the thousand separator and use it in the watch window for your debugging purpose. After you are done with debugging, you can remove this array.

int[] numbers = { 100000, 300000 };
string[] stringNumbers = numbers.Select(n => n.ToString("N")).ToArray();

You can watch stringNumbers here in the watch window.

As noted in the other answers, it's not directly possible. So until the feature request lands in Visual Studio, you need to manually do it. So to sum up, you can:

  • use DebuggerDisplayAttribute to change the way members of an object are shown (see this blog entry )
  • add a new variable with the string representation in your code (this is quite dirty as you'll want to remove it once you're done debugging)
  • format the data in the Watch window (so your first need to add your data in the watch window, and it won't work when you hover over your data). Eg
// For a long variable
myLong.ToString("N0")
// For a long array variable (that might slow VSNet down if you have tons of rows)
myLongArray.Select(l => l.ToString("N0"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM