如何在导出到csv并以excel打开时保持十进制精度?

时间:2021-09-04 15:29:48

I have exported the values to a csv file in c#.

我已经将值导出到c#中的csv文件中。

The csv file has all 5 decimal places present for each value, yet when opened with excel, the decimal places are reduced to two for each one.

csv文件的每个值都有5位小数,但是当用excel打开时,小数点后的位置就变成了两个。

Is there a way of programatically ensuring that the decimal places are kept when the csv is opened in excel?

是否有一种程序上确保在excel中打开csv时保留小数部分的方法?

var nFI = new NumberFormatInfo();

nFI.NumberDecimalDigits = precision;
line += (CsvEscape(((decimal)dr[colName]).ToString("N", nFI)) + ",");

This is how the decimals are exported to the csv.

这就是小数输出到csv的方式。

2 个解决方案

#1


1  

When outputting to a csv file, to preserve the decimal places, wrap the value in double quotes and prefix a '=' sign. (Excuse the code, its still in prototype form)

当输出到csv文件时,为了保留小数点的位置,将值包装为双引号和前缀a '='符号。(不好意思,代码还是原型)

line += ("=" + '"' (((decimal)dr[colName]).ToString("N",nFI).Replace(",",""))+ '"' + ",");

This outputs as ="63000.00000",

这个输出= " 63000.00000 ",

#2


0  

Are you using a format provider (NumberFormatInfo) for any other purpose? If you omit the formatting parameters in ToString() it should output as-is.

您是否将格式提供程序(NumberFormatInfo)用于其他目的?如果省略ToString()中的格式参数,则应该按原样输出。

#1


1  

When outputting to a csv file, to preserve the decimal places, wrap the value in double quotes and prefix a '=' sign. (Excuse the code, its still in prototype form)

当输出到csv文件时,为了保留小数点的位置,将值包装为双引号和前缀a '='符号。(不好意思,代码还是原型)

line += ("=" + '"' (((decimal)dr[colName]).ToString("N",nFI).Replace(",",""))+ '"' + ",");

This outputs as ="63000.00000",

这个输出= " 63000.00000 ",

#2


0  

Are you using a format provider (NumberFormatInfo) for any other purpose? If you omit the formatting parameters in ToString() it should output as-is.

您是否将格式提供程序(NumberFormatInfo)用于其他目的?如果省略ToString()中的格式参数,则应该按原样输出。