我对C#完全不熟悉,所以请耐心听我说。
private static string GetFormattedValue(string dataType, dynamic cellValue)
{
string formattedCellValue = string.Empty;
if (cellValue == null)
cellValue = DBNull.Value;
if (dataType == "STRING")
{
formattedCellValue = string.Format("'{0}',", cellValue);
}
else if (dataType == "NUMBER")
{
if (string.IsNullOrEmpty(Convert.ToString(cellValue)))
cellValue = 0;
formattedCellValue = string.Format("'{0}',", cellValue.ToString("F17"));
}
else if (dataType == "DATE")
{
formattedCellValue = string.Format("'{0}',", cellValue);
}
else
{
formattedCellValue = string.Format("'{0}',", cellValue.ToString("F17"));
}
return formattedCellValue;当dataType是number而cellValue是一个整数时,我得到一个错误消息:“匹配string.ToString(System.IFormatProvider)的最佳重载方法有一些无效参数”
cellValue通常是非常小的数字,如果没有"F17",将作为科学记数法返回(这会导致另一个错误),但也会返回导致上述错误的整数。
这不是我的代码,我只是在运行它,并且知道的知识足以逐步通过它。你知道如何判断cellValue是否可以被读取来判断整数是否为整数吗?或者其他更好的建议?
发布于 2013-04-05 10:34:14
有许多方法尝试将字符串解析为数字,例如Double.TryParse
发布于 2013-04-05 10:35:10
F17引用双精度数据类型...不是字符串。
试试这个,让我知道:
else if (dataType == "NUMBER")
{
if (string.IsNullOrEmpty(Convert.ToString(cellValue)))
cellValue = 0;
formattedCellValue = string.Format("'{0}',", Convert.ToDouble(Convert.ToString(cellValue)).ToString("F17"));
}https://stackoverflow.com/questions/15824895
复制相似问题