0

我得到字符串值,如123.00000000.
现在我只想取 123.00 的值。那么如何删除除值后两位数之外的所有最后一位数字。
例如:

textBox1.Text="123.00000000"; 我想textBox1.Text="123.00"; 提前谢谢!

4

3 回答 3

3

使用正确的字符串格式。

double value = double.Parse("123.00000000", CultureInfo.InvariantCulture);
textBox1.Text = value.ToString("N2");

标准数字格式字符串

演示

编辑string.Substring问题是世界上只有一半的人使用.小数分隔符。因此,您需要了解输入从数字转换为字符串的文化。如果它是您可以使用的同一台服务器CultureInfo.CurrentCulture(或省略它,因为这是默认设置):

double originalValue = 123;
// convert the number to a string with 8 decimal places
string input = originalValue.ToString("N8");
// convert it back to a number using the current culture(and it's decimal separator)
double value = double.Parse(input, CultureInfo.CurrentCulture);
// now convert the number to a string with two decimal places
textBox1.Text = value.ToString("N2");
于 2012-12-11T09:42:02.467 回答
2
string str = "123.00000000";
textBox1.Text = str.Substring(0,str.IndexOf(".")+3);
于 2012-12-11T09:42:27.943 回答
1

有关如何格式化数字的完整概述,请参见此处。在您的情况下,它是:

textBox1.Text = String.Format("{0:0.00}", 123.0);
于 2012-12-11T09:36:14.930 回答