-2

I am getting the following error on my code : "Input String was not in a correct format". on the line following line:

Convert.ToDecimal(tdprice.InnerText).ToString("##.###");

In the code block:

protected void rptConsole_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HtmlTableCell tdprice = e.Item.FindControl("price") as HtmlTableCell;
            tdprice.InnerText = Convert.ToDecimal(tdprice.InnerText).ToString("##.###");
        }
    }

To get a full understanding of what I am trying to do please visit the following link Selecting an Item or column in a Repeater and changing the Data

Community
  • 1
  • 1
user3668266
  • 173
  • 1
  • 3
  • 15

2 Answers2

1

Convert.ToDecimal(string) method uses Decimal.Parse(string, CurrentCulture) explicitly. Here how it's implemented;

public static decimal ToDecimal(String value)
{
    if (value == null)
        return 0m;
    return Decimal.Parse(value, CultureInfo.CurrentCulture);
}

And Decimal.Parse(string, CurrentCulture) method implemented as;

public static Decimal Parse(String s, IFormatProvider provider)
{
    return Number.ParseDecimal(s, NumberStyles.Number, NumberFormatInfo.GetInstance(provider));
}

As you can see, it uses NumberStyles.Number enumeration. This is a composite style which can contains;

Since you said your method fails with 5.33432 value, most probably your CurrentCulture's NumberDecimalSeparator property is not .

You can use .Clone() method for create a copy CultureInfo based on your CurrentCulture and set this property to .

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

You don't need to use OnItemDataBound event for that at all.

You can do it like this in ASP markup:

...
<td align="center"><%# String.Format("{0:F2}", Eval("[price]")) %></td>
...

Or if you want to use .NET currency formatting:

...
<td align="center"><%# String.Format("{0:C}", Eval("[price]")) %></td>
...

Check this article for more specific information about .NET Standard Numeric Format String: http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

Henri Hietala
  • 3,021
  • 1
  • 20
  • 27