Convert strings to decimal with format

I need to convert a String to a decimal character in C #, but this string has different formats.

For instance:

"50085"

"500.85"

"500.85"

This should be converted to 500.85 in decimal format. Is there a simplified form for this conversion using the format?

+6
source share
6 answers

While decimal.Parse () is the method you are looking for, you will have to provide it with a bit more information. It will not automatically choose between the three formats that you give, you will need to specify which format you expect (in the form of IFormatProvider). Note that even with IFormatProvider, I don’t think that β€œ50085” will be properly retracted.

The only thing I see is that from your examples you can see that you always expect two decimal places of precision. If so, you can remove all periods and commas, and then divide them 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; } 
+14
source

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

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

This gives an answer of 501 when the stream culture is set to a culture in which the comma is used as a floating point. You can force it like this:

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

This will work depending on your culture settings:

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

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

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

But you will need to check for several . ... this seems to boil down to most of the input disinfection problem. If you can check and misinform the input to all the rules, decimal conversion will be much easier.

+6
source

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 give a value of 500.85, I will assume that the culture is French, and therefore 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, then change the culInfo constructor.

+3
source

There are many ways:

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

Make sure you try to catch ...

+1
source

This is a new feature called Character Grouping .

Steps:

  • Open region and language in the control panel
  • Click Advanced Setup
  • Numbers Tab
  • Set the Digit Grouping Symbol as your 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; 
+1
source

Source: https://habr.com/ru/post/887817/


All Articles