mzurita
mzurita

Reputation: 690

How to correctly store text as a number in Excel cell

After searching other topics on the site and did not find the right answer I open my own thread. If it exists and I have not seen please comment.

I create a Excel worksheet programmatically through C# (using Microsoft.Office.Interop). I fill the cells with a soap request, blah blah blah.

The issue is, for each field I have the value (currency) as a string, with the following formats X, X,X or X,XX (spanish culture). So when I fill each field with the text, if the string have the "X" format it is store as number, otherwise I have the warning message "Number stored as text"

So I have tried converting the string to number and then I fill the cell, which has a currency format, and the result is right in the excel sheet, but the cell value was wrong rounded.

I just need convert from the string to a decimal value with only 2 ciphers.

xlRng = xlWorkSheet.get_Range("A2", "A100");
xlRng.Style.NumberFormat = "0,00";

xlWorkSheet.Cells[2,1] = Convert.ToSingle(stringWithValue);

For example:

Upvotes: 1

Views: 1629

Answers (1)

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

Stay away from doubles and singles if possible.

`= Decimal("86,72",10) where 10 is base 10

should do it.

Upvotes: 1

Related Questions