2

如果双数或浮点数全为零,如何隐藏小数部分。我正在将浮点数转换为字符串及其显示尾数部分,即使它为零。例如:

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 中有没有内置的解决方案来完成它?谢谢

4

1 回答 1

3

尝试使用double.ToString()格式字符串的重载,并传递它"R"

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

double secondNumber = 123.2234;
string strSecondNumber = secondNumber.ToString("R");
于 2013-07-17T09:50:30.017 回答