文本框输出大小

本文关键字:输出 文本 | 更新日期: 2023-09-27 18:29:51

首先,我在网上搜索问题的答案,除了MaxLength属性的输入限制之外,我找不到任何东西。如果对此有答案,我深表歉意。。

我在限制或格式化文本框中的"输出"时遇到了问题,而不是输入(textbox.MaxLength)。。例如,我计算了一些双类型26899894462的数字。。我想把这个显示大小限制为最多5位数,所以它看起来像268,99。我该怎么做?

谢谢。。

文本框输出大小

如果您用程序设置值,您可以设置字符串格式:

textbox1.Text = someValue.ToString("0.00"); 

另一种方法是使用NumericUpDown控件(而不是文本框)并设置其属性DecimalPlaces

这个怎么样-

    double d; //say d is the double var you got calculated after whatever process.
    string dFormatted = d.ToString(); //you can add custom formatting to see commas or decimals here.
    TextBox1.Text = dFormatted.Substring(0, Math.Max(5, dFormatted.Length));