0

I am trying to print out a double to excel. In my GUI the number prints out correctly.

But when I write the number to an excel file the number comes out with out the zeros after the decimal place. For examples the number 1.10 prints out correctly in my GUI, but in excel in prints out 1.1.

double foo = 1.10;
writeline(foo.ToString("F2"));

I have tried String.Format("{0:0.00}", foo.ToString("F2")); without resolving this issue. I am pretty sure the problem is when the number gets into excel, excel truncates all zeros after the decimal place. Is there a way to send excel a message to print out two places after the decimal?

I am working on someone elses code that opens and closes the excel file. They are using File.Create and StreamWriter to create and open the excel file

4

2 回答 2

2
String.Format("{0:0.00}", foo.ToString("F2"));

不起作用,因为您正在尝试格式化已经转换为字符串的内容。使用直接变量 - 你的 foo 应该返回一个数值,然后你就可以像这样使用它(示例):

String.Format("{0:0.00}", foo.GetDouble("F2"));

换句话说:

String.Format("{0:0.00}", "1.10");    //wrong
String.Format("{0:0.00}", 1.10d);     //correct
于 2014-02-06T14:24:47.813 回答
0

对于一个简单的问题,我已经看到了太多复杂的答案,我也遇到了同样的问题,这就是我来到这里但没有得到任何合理答案的原因。我想通了,只需突出显示该列,右键单击它----格式化单元格-----会出现一个对话框,进入类别---数字,然后添加任意数量的小数位.

于 2016-01-10T19:33:26.147 回答