This question already has answers here:
How do I format a double to a string and only show decimal digits when necessary?
                                
                                    (2个答案)
                                
                        
                                6年前关闭。
            
                    
如果全为零,如何隐藏双精度或浮点数的小数部分。我将浮点数转换为字符串,并显示其尾数部分,即使该数字为零。例如:

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中是否有任何内置解决方案可以完成它?
谢谢

最佳答案

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

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

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

10-04 20:52