如果它是零,如何隐藏小数部分?

时间:2013-07-17 09:40:50

标签: c# math

如果全部为零,如何隐藏double或float数的小数部分。我正在将浮点数转换为字符串,并且即使它为零,它也会显示Mantissa部分。例如:

double number = 123.00;
string strNumber = number.ToString(); // it shows "123.0", what I need is only "123"

double secondNumber = 123.2234;
string strSecondNumber = secondNumber.ToString(); // it shows "123.2234" as needed.

.NET中是否有内置的解决方案来完成它? 感谢

1 个答案:

答案 0 :(得分:3)

尝试使用带有格式字符串的double.ToString()重载,并将其传递给"R"

double number = 123.00;
string strNumber = number.ToString("R"); 

double secondNumber = 123.2234;
string strSecondNumber = secondNumber.ToString("R");