Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTable Compute Value is too large or too small for type Int32

Tags:

c#

datatable

I recently came across a way to evaluate expression in C#, using the compute method of a datatable object. Here is a piece of code :

    string expression = "330200000*450000";
    var loDataTable = new DataTable();
    var loDataColumn = new DataColumn("Eval", typeof(double), expression);
    loDataTable.Columns.Add(loDataColumn);
    loDataTable.Rows.Add(0);
    MessageBox.Show(((double)(loDataTable.Rows[0]["Eval"])).ToString()); 

If you put a simple expression like "300*2" this will work, however an expression returning a large number would not work, I get the message :

"Value is either too large or too small for Type 'Int32'."

I tried to force the type to double, but for some reason the error still points to something regarding Int32 type which I am not sure where it comes from.

A little hand on that ?

like image 551
Romain Avatar asked Apr 10 '15 19:04

Romain


People also ask

How can you store 2181003000 in INT data type?

You can not store big no in small. -2,147,483,648 .. 2,147,483,647 is the range of int datatype; so how can you store 2181003000 in int!! Its like try to put 1.5 liter into 1 liter can.

Does the server know the total amount of data in DataTables?

Conceivably the server is still compiling the data even as it's passing information to DataTables, so it wouldn't actually know the total amount of data in the data set. Am I thinking about this the wrong way? Does the server actually have to generate the FULL report prior to starting to send information to DataTables?

What is the maximum value an int can store in Java?

An int can store values between -2^32-1 to 2^32. To store anything larger, use long data type which can store between -2^64-1 to 2^64. int b = Convert.ToInt64 (long) (TextBoxTL.Text);.

Why is my data waiting so long in DataTables?

The issue with that is sometimes we have big data sets, and then we have to wait for our server to first build the static file, and then wait again for the data to appear inside DataTables. So, it ultimately results in a lot of 'waiting'.


1 Answers

Append ".0" to the values in your equation:

string expression = "330200000.0*450000.0";

If, as you said, the equation has been entered as it is (i.e. without the ".0") by the user, then you might have to engage in some vaguely unpleasant string manipulation to achieve this. I came up with the following, although I'm certain it can be done better:

string expression = "330200000*450000"; // or whatever your user has entered
expression = Regex.Replace(
    expression, 
    @"\d+(\.\d+)?", 
    m => {
         var x = m.ToString(); 
         return x.Contains(".") ? x : string.Format("{0}.0", x);
    }
);
var loDataTable = new DataTable();
var computedValue = loDataTable.Compute(expression, string.Empty);

The regular expression attempts to account for whether or not your user has already put a decimal on the end of any of the numbers within their equation.

like image 121
Gibboniser Avatar answered Nov 14 '22 23:11

Gibboniser