I am using EPPlus to read excel data using c#. Excel contains data like below.
Amt
-125,949.00
-909,123.21
I am trying to read the exact data in variable using below mentioned code, but it's not returning the exact value including ',' and '.00'. If value is '-125,949.00' then its returning '-125949'. If value is '-909,123.21' then is returning '-909123.21'.
using (var package = new ExcelPackage(fileStr))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
int rowCount = worksheet.Dimension.End.Row;
for (int row = 2; row <= rowCount; row++)
{
var amt = worksheet.Cells[row,1].Value;
}
}
So how do I actually get the exact value including ',' and '.00' and save it in variable?
I tried searching online and tried converting to double or decimal, but I got same result.
I tried using .Text but its returning '0'.
var amt = worksheet.Cells[row,1].Text
The reason I want exact value is I need to perform some validations later. e.g. If Amt is other than Decimal, then I need to show some error message. As I am getting -125949 in variable, so I am not able to perform the validation.
The symbols like ,
and .
are formatting elements - 1000th and decimal separators accordingly. The numbers -125,949.00
and -909,123.21
usually are saved as numbers. Therefore, to see in amt
what you are expecting it's necessary to convert these numbers to string using formatter:
var amt = ((double)worksheet.Cells[row,1].Value).ToString("#,0.00");
Now you will see what you are expecting.