In C#, how can I display only a certain number of digits?
In my application, I have a TextBlock that I display a Double number in after the user presses a button. This number can be a very small decimal or a very large number needing exponential notation (i.e. 3.43e12). The problem is, the program prints so many digits that it overflows my TextBlock and the user can't see all the valid information.
So how can I restrict the Double to print so to not overflow the TextBlock?
The code I am using to set the text is:
theTextBox.Text = (split * input).ToString();
EDIT: Someone asked for specific examples, so I thought I would clarify something. I b开发者_如何学Goasically want the string to never be longer than, say, 10 characters. That way it will fit in the TextBlock. I guess the trick is, when should those 10 characters be decimal places, whole numbers, or scientific notation that is the trick...
Use Double.ToString(String)
, giving an appropriate format specifier, as described at http://msdn.microsoft.com/en-us/library/kfsatb94.aspx.
Have a looksee here http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
You can put your format string in as a param to the ToString method
精彩评论