Convert and multiply currency string eval values in an ASPX page

666 views Asked by At

I am trying to convert two string values to integers, and multiply them, to use in a ListView on an ASPX page.

The following code doesn't work, but it should give you an idea of what I am trying to accomplish:

<%#String.Format("{0:c}", Convert.ToInt32(Eval("nbrQtyOrdered")) * Convert.ToInt32(Eval("curSellPrice"))) %>

I am getting the following error:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

Additional information: Input string was not in a correct format.

nbrQtyOrdered is a string representation of an integer value and curSellPrice is a string representation of a double written as "$22.22", for example. I am assuming now the dollar sign is giving me the error.

How can I fix this problem and achieve what I am trying to do?

1

There are 1 answers

3
Tot Zam On

You are correct. "$22.22" with a dollar sign cannot be converted to an Integer.

I would suggest first trimming the dollar sign. This can be done multiple ways:

Eval("curSellPrice").Replace("$", "")

OR

Eval("curSellPrice").TrimStart('$');

Once you remove the dollar sign from the curSellPrice string, you should be able to use it with Convert.ToInt32() .