Convert strings to decimal while preserving the exact input format

I'm sure this is a piece of cake, but I'm really struggling with something that seems trivial.

I need to check the entered text of the text field on the submit form and check if it is within the required range (I tried checking the range, but for some reason this does not work, so I'm trying to do this on the server side).

What I want to do:

Get the entered value (for example, 0.02), replace the commas and periods, convert them to decimal (or double or equivalent) and check if it is between 0.10 and 35000.00.

Here is what I still have:

string s = txtTransactionValue.Text.Replace(",", string.Empty).Replace(".", string.Empty); decimal transactionValue = Decimal.Parse(s); if (transactionValue >= 0.10M && transactionValue <= 35000.00M) // do something 

If I pass 0.02 to the above, transactionValue is 2. I want to save the value as 0.02 (i.e. do not make any changes to it) - 100.00 - 100.00, 999.99 - 999.99)

Any ideas?

Thanks in advance, Brett

+4
source share
6 answers

As already mentioned, you throw out periods and commas. Perhaps you do this because you have some problems depending on the language that the user has, because in different cultures the dot and comma have a different meaning.

So, if you know the user language, you can try one of them:

 decimal value = Decimal.Parse("0.02", CultureInfo.GetCultureInfo("en-US")); decimal value = Decimal.Parse("0,02", CultureInfo.GetCultureInfo("de-DE")); 
0
source

You replace any occurrences of "." or "," with String.Empty, leaving the string without any delimiters at all. It is better to use decimal.Parse() or decimal.TryParse() with the corresponding CultureInfo and NumberStyles parameters.

+3
source

You replace "." with nothing, so your input goes from "0.02" to "002", which will be parsed as 2.0.

Why are you replacing commas and periods (decimal points)?

+2
source

You replace the "dot". This means that “0.02” will be “002”, which will parse as “2”.

Just don't replace the dot. and I think comma replacement is not needed.

0
source

You can also make a simple method to take into account the CultureInfo and NumberStyles you want. Here is my suggestion:

  public decimal ParseDecimal(string input) { CultureInfo cultureInfo = null; NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands; decimal value = 0; string[] userLanguages = HttpContext.Current.Request.UserLanguages; if (userLanguages.Length > 0) { cultureInfo = new CultureInfo(userLanguages[0]); Decimal.TryParse(input, style, cultureInfo, out value); } return value; } 
0
source

I would use Decimal.Parse("0.02", CultureInfo.InvariantCulture) , but this only works if you use . as a viewer. To ensure that you are not getting any errors, you should always check your data before analyzing it.

Decimal.Parse seems to ignore unknown characters, while other Parse functions throw exceptions.

0
source

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


All Articles