[英]Double ToString() no scientific notation not returning a string for 0.0
I need to return the string representation of a double in decimal notation rather than scientific and I used a modified version of the accepted solution here: 我需要以十进制表示法返回double的字符串表示形式而不是科学表达式,我在此处使用了已接受解决方案的修改版本:
Double ToString - No Scientific Notation Double ToString - 没有科学记数法
private static readonly string format = "." + new string('#', 324);
foo.ToString(format);
This works well other than for a value that equals zero ie 0, 0.0, 0.000 etc where it returns an empty string. 这适用于除了等于零的值,即0,0.0,0.000等,它返回一个空字符串。
What would be the easiest way to return a string representing 0 for any value of zero. 对于任何零值返回表示0的字符串最简单的方法是什么。
I don't mind if the string is 0 or 0.0 etc as long as it is numerical and not scientific. 我不介意字符串是0还是0.0等,只要它是数字而不是科学的。
Edit: I know I could add a conditional statement to check if the string is empty but I am interested to see if there is a way to do it using string formatting. 编辑:我知道我可以添加一个条件语句来检查字符串是否为空但我有兴趣看看是否有办法使用字符串格式化。
You could always add a statement like: 您总是可以添加如下语句:
foo.ToString(format)
if (string.IsNullOrEmpty(foo))
foo = "0";
Maybe it's what you are looking for: 也许这就是你要找的东西:
double aa = 12.33333;
string foo = String.Format("{0:0.#}", System.Convert.ToSingle(aa));
Argh, those hacks people use. 唉,人们使用的那些黑客。 To give one hack more here:
在这里给一个黑客更多:
foo.ToString(format + ";" + "-" + format + ";" + "0");
or with 或者
static readonly string newFormat = format + ";" + "-" + format + ";" + "0";
defined under format
in the code source, just: 在代码源中的
format
下定义,只是:
foo.ToString(newFormat);
Of course that is a constant, so it is really: 当然这是一个常数,所以它确实是:
foo.ToString(.####################################################################################################################################################################################################################################################################################################################################;-.####################################################################################################################################################################################################################################################################################################################################;0);
Hope you like it (I don't). 希望你喜欢它(我不喜欢)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.