简体   繁体   中英

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 (ie 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 basically 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...

使用Double.ToString(String) ,提供适当的格式说明符,如http://msdn.microsoft.com/zh-cn/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

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