将数据表中的指数字符串转换为小数 Select
Converting exponential string to decimal in Datatable Select
我正在尝试将指数字符串值转换为十进制并将其与数据表 Select
函数中的另一个十进制值进行比较。
var dt = new DataTable();
dt.Columns.Add("Salary", typeof(string));
dt.Rows.Add("8.83567E-05");
string userInput = "8.83567E-05";
var matchedRows = dt.Select("Convert([Salary],'System.Decimal') = " + Decimal.Parse(userInput, System.Globalization.NumberStyles.Float));
但是我得到以下错误
System.FormatException: 'Input string was not in a correct format.'
所以经过一些研究,我发现使用 System.Double
将适用于我的情况
var matchedRows = dt.Select("Convert([Salary],'System.Double') = " + Decimal.Parse(userInput, System.Globalization.NumberStyles.Float));
它奏效了。
但我不想使用 double
,因为它大约存储数据。我的数据最多可以保留 10 位小数。
有没有办法将字符串指数字符串值转换为十进制数据类型?还是在我的情况下使用 double 安全?
根据microsoft documentation,使用double是正确的方法。
(Expression
属性 使用与 DataTable
的 Select
方法中的过滤器相同的语法)
Real literals using scientific notation, such as 4.42372E-30, are
parsed using System.Double.
Real literals without scientific notation, but with a decimal point,
are treated as System.Decimal. If the number exceeds the maximum or
minimum values supported by System.Decimal, then it is parsed as a
System.Double. For example:
142526.144524 will be converted to a Decimal.
345262.78036719560925667 will be treated as a Double.
我正在尝试将指数字符串值转换为十进制并将其与数据表 Select
函数中的另一个十进制值进行比较。
var dt = new DataTable();
dt.Columns.Add("Salary", typeof(string));
dt.Rows.Add("8.83567E-05");
string userInput = "8.83567E-05";
var matchedRows = dt.Select("Convert([Salary],'System.Decimal') = " + Decimal.Parse(userInput, System.Globalization.NumberStyles.Float));
但是我得到以下错误
System.FormatException: 'Input string was not in a correct format.'
所以经过一些研究,我发现使用 System.Double
将适用于我的情况
var matchedRows = dt.Select("Convert([Salary],'System.Double') = " + Decimal.Parse(userInput, System.Globalization.NumberStyles.Float));
它奏效了。
但我不想使用 double
,因为它大约存储数据。我的数据最多可以保留 10 位小数。
有没有办法将字符串指数字符串值转换为十进制数据类型?还是在我的情况下使用 double 安全?
根据microsoft documentation,使用double是正确的方法。
(Expression
属性 使用与 DataTable
的 Select
方法中的过滤器相同的语法)
Real literals using scientific notation, such as 4.42372E-30, are parsed using System.Double.
Real literals without scientific notation, but with a decimal point, are treated as System.Decimal. If the number exceeds the maximum or minimum values supported by System.Decimal, then it is parsed as a System.Double. For example:
142526.144524 will be converted to a Decimal.
345262.78036719560925667 will be treated as a Double.