开发者

Convert string to decimal with format

I need convert a 开发者_运维百科String to a decimal in C#, but this string have different formats.

For example:

"50085"

"500,85"

"500.85"

This should be convert for 500,85 in decimal. Is there is a simplified form to do this convertion using format?


Some cultures use a comma to indicate the floating point. You can test this with the following code on an aspx page:

var x = decimal.Parse("500,85");
Response.Write(x + (decimal)0.15);

This gives the answer 501 when the thread culture has been set to a culture that uses the comma as floating point. You can force this like so:

var x = decimal.Parse("500,85", new NumberFormatInfo() { NumberDecimalSeparator = "," });


While decimal.Parse() is the method you are looking for, you will have to provide a bit more information to it. It will not automatically pick between the 3 formats you give, you will have to tell it which format you are expecting (in the form of an IFormatProvider). Note that even with an IFormatProvider, I don't think "50085" will be properly pulled in.

The only consistent thing I see is that it appears from your examples that you always expect two decimal places of precision. If that is the case, you could strip out all periods and commas and then divide by 100.

Maybe something like:

public decimal? CustomParse(string incomingValue)
{
    decimal val;
    if (!decimal.TryParse(incomingValue.Replace(",", "").Replace(".", ""), NumberStyles.Number, CultureInfo.InvariantCulture, out val))
        return null;
    return val / 100;
}


This will work, depending on your culture settings:

string s = "500.85";
decimal d = decimal.Parse(s);

If your culture does not by default allow , instead of . as a decimal point, you will probably need to:

s = s.Replace(',','.');

But will need to check for multiple .'s... this seems to boil down to more of an issue of input sanitization. If you are able to validate and sanitize the input to all conform to a set of rules, the conversion to decimal will be a lot easier.


Try this code below:

string numValue = "500,85";
System.Globalization.CultureInfo culInfo = new System.Globalization.CultureInfo("fr-FR");
decimal decValue;
bool decValid = decimal.TryParse(numValue, System.Globalization.NumberStyles.Number, culInfo.NumberFormat, out decValue);
if (decValid)
{
    lblDecNum.Text = Convert.ToString(decValue, culInfo.NumberFormat);
}

Since I am giving a value of 500,85 I will assume that the culture is French and hence the decimal separator is ",". Then decimal.TryParse(numValue, System.Globalization.NumberStyles.Number, culInfo.NumberFormat,out decValue); will return the value as 500.85 in decValue. Similarly if the user is English US then change the culInfo constructor.


There are numerous ways:

  1. System.Convert.ToDecimal("232.23")
  2. Double.Parse("232.23")
  3. double test; Double.TryParse("232.23", out test)

Make sure you try and catch...


This is a new feature called Digit Grouping Symbol.

Steps:

  1. Open Region and Language in control panel
  2. Click on Additional setting
  3. On Numbers tab
  4. Set Digit Grouping Symbol as custom setting. Change comma; replace with (any character as A to Z or {/,}). Digit Grouping Symbol=e;

Example:

string checkFormate = "123e123";
decimal outPut = 0.0M;
decimal.TryParse(checkFormate, out outPut);
Ans: outPut=123123;


Try This

 public decimal AutoParse(string value)
    {
        if (Convert.ToDecimal("3.3") == ((decimal)3.3))
        {
            return Convert.ToDecimal(value.Replace(",", "."));
        }
        else
        {
            return Convert.ToDecimal(value.Replace(".", ","));
        }

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜