I have a float
number: 10071072872302
and I need to convert it to decimal
.
float number = 10071072872302f;
var convertNumber = Convert.ToDecimal(number);
I get the value: 10071070000000
which is Wrong.
If I convert it to double
first, and then convert it to decimal
:
float number = 10071072872302f;
var convertNumber = Convert.ToDecimal(Convert.ToDouble(number));
I get the value: 10071073357824
which is also Wrong.
If I just convert float
to double
:
float number = 10071072872302f;
var convertNumber = Convert.ToDouble(number);
I get the value: 10071073357824
which is Wrong.
I am really confused. My company's database is designed with decimal
data type. I need to programmatically calculate the value of the data according to the pre-configured formula, I need to use the NCalc
library which returns the float data type. Unfortunately, I can't convert float
to decimal`.
Could anyone help me with this impasse?
EDIT I want to clarify why I need to convert float to decimal.
To get a value, I need to calculate the expression like: ISA16+ISA8+ISNULL(MAX((ABS(BSMS10)-ABS(BSMS10_)),0),0)
After taking the data in the database to replace the expression, I have an expression like this: 10071072872302+872302+MAX((ABS(-072872302)-ABS(2302)),0)
To calculate the value of this expression, I use the NCalc
library
private static object CalculateString(string formula)
{
var expression = new Expression(formula);
expression.EvaluateFunction += (name, args) =>
{
switch (name)
{
case "ABS":
args.Result = Math.Abs(Convert.ToDouble(args.Parameters[0].Evaluate()));
break;
case "MAX":
args.Result = Math.Max(Convert.ToDouble(args.Parameters[0].Evaluate()), Convert.ToDouble(args.Parameters[1].Evaluate()));
break;
}
};
var value = expression.Evaluate();
return value;
}
Expressions come in many forms. However, to check the value, I tried checking with a number of 10071072872302
var result = CalculateString("10071072872302");
The result I get is a float value, so I need to convert it to decimal to match the data type in the database. That is why I posed the question above.