Bug in Decimal.Parse() | ????

Discussion in 'ASP.NET / ASP.NET Core' started by RRed, Aug 19, 2009.

  1. Hi,

    I have a issue with parsing a decimal in Asp.net.

    /*
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Globalization;

    public partial class Default2 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    decimal sum = 0;
    string amount = "1,11";

    for (int i = 0; i < 4; i++)
    {
    sum += decimal.Parse(amount, NumberStyles.Any);
    }

    string count2Str = string.Format("{0}", sum);

    Response.Write(sum);
    }
    }
    */

    Running the code on localhost results in : 4,44 :)
    Running the code on Discountasp.net results in : 444 :confused:

    Is the issue generally know? Is there a workAround for this issue?

    Thanks in advance
     
  2. ...Have you tried it this way?
    decimal.Parse(stringValue.Replace(',', '.'), NumberStyles.Any, amount);

    ...Where you can filter/replace as needed?
    All the best,
    Mark
     
  3. Yes, something like that, unfortunately no luck :-(
    I will give it a second try..

    Update:

    Second try no good..

    FYI

    NumberStyles.Any must allow the style AllowThousands.

    NumberStyles.AllowThousands says:

    Indicates that the numeric string can have group separators, for example, separating the hundreds from the thousands.
    Valid group separator characters are determined by the NumberGroupSeparator and CurrencyGroupSeparator properties
    of NumberFormatInfo and the number of digits in each group is
    determined by the NumberGroupSizes and CurrencyGroupSizes properties of NumberFormatInfo.
     
  4. Workaround:

    /*
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Globalization;
    public partial class Default2 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    decimal sum = 0;
    string amount = "1,11";
    int charPos = 0;
    int parameter = -2;//| TextBox() MaxLength

    for (int i = 0; i < 4; i++)
    {
    sum += decimal.Parse(amount, NumberStyles.Any);
    }
    string count2Str = string.Format("{0}", sum);

    foreach (char c in count2Str)
    {
    charPos++;
    }
    charPos += parameter;
    count2Str = count2Str.Insert(charPos, ",");
    Response.Write(count2Str);
    }

    }
    */
    Running the code on localhost results in : 4,,44 :cool:
    Running the code on Discountasp.net results in : 4,44 :)
     
  5. ...Cool ;-)
     

Share This Page