如果全部为零,如何隐藏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中是否有内置的解决方案来完成它? 感谢
答案 0 :(得分:3)
尝试使用带有格式字符串的double.ToString()
重载,并将其传递给"R"
:
double number = 123.00;
string strNumber = number.ToString("R");
double secondNumber = 123.2234;
string strSecondNumber = secondNumber.ToString("R");