简体   繁体   中英

(C#) How to find a cell from the axis of a DataTable / Divide a number Into two

I created a DataTable from the Normal Distribution Table. The first row and column of my DataTable are the two axis (See image linked below)

I need to create a function that find the corresponding number from this Data Table depending on the number I ask for.

Exemple: If I ask for 0,92 the Function should seperate the number in 0,9 and 0,02 (the addition of both gives us our initial number) and checking the axis it finds the number in that position.

Since I can't explain clearly here's a picture of what it needs to do.

在此处输入图片说明

The Yellow cells are 0,92 separated into two axis and the red cell the return value I need.

The two things I don't know how to do:

  • Seperating the initial number (0,92)
  • Finding a cell using DataTable's first row and column (0,9 and 0,02)

Oh and, (0,0) is -1

You can try this:

// input:
double d = 0.87d;
// convert to integer
int i1 = (int)(d * 100);
// all but the last digit
int i2 = i1 / 10;
// last digit alone
int i3 = i1 - (i2 * 10);

i3 should be the column index for your target cell and i2 the row:

 double t = yourDataTable.Rows[i2].Field<double >(i3);

A few notes:

  • Not sure what you mean by (0,0) is -1 but you can always add an if clause for a special case or maybe modify the table content..

  • In case the headers you show are actually part of the table data you will nedd to add an offset, of course..

  • And if the rows go beyond 99,9 you should use not 100 but a larger power of 10 ..

  • Also note that the seemingly clear fractional digits do not actually exist in binary numbers; therefore going to integer is a simple way to avoid the complications that can result when using calculations in floats or doubles or from going via strings . Using decimal may be an alternative, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM