Converting Request QueryString to Decimal - c#

I can not convert query string to decimal.
In this example, when I control Request.QueryString["Amount"] value, It is 32.52 After the below code works, The Amount values is 3252M like that. How can I easily do this?
decimal Amount= 0;
if (Request.QueryString["Amount"] != null)
Amount = Convert.ToDecimal(Request.QueryString["Amount"]);

Convert.ToDecimal uses your current culture settings by default.
I strongly suspect your CurrentCulture's NumberDecimalSeparator property is not ., but NumberGroupSeparator property is .
That's why your program thinks this is a thousands separator, not decimal separator and it parses as a 3252, not 32.52.
As a solution you can use a culture which have . as a NumberDecimalSeparator like InvariantCulture, or you can .Clone your current culture and set it's NumberDecimalSeparator to . 1
Amount = Convert.ToDecimal(Request.QueryString["Amount"], CultureInfo.InvariantCulture);
or
var culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
culture.NumberFormat.NumberDecimalSeparator = ".";
culture.NumberFormat.NumberGroupSeparator = ",";
Amount = Convert.ToDecimal("32.52", culture);
1: If your current culture's thousands separator is not . already. Otherwise, you need to change it as well. Both property can't have the same values for any culture

I think you are having problems with the Culture as stated in the rpevious answer. You may want to try using different cultures:
RetVal = decimal.Parse(Request.QueryString["Amount"], CultureInfo.CurrentCulture);
Then I would try:
RetVal = decimal.Parse(Request.QueryString["Amount"], CultureInfo.InvariantCulture);

Related

How to convert string into decimal?

I text box i have a value:
1212,12
Additionally my text box have a mask:
MaskType=Numeric
EditMask='n2'
i try to parse it:
var culture = CultureInfo.CreateSpecificCulture("en-US");
var value = decimal.Parse(myTextBox.Text, culture);
but get value=121212 when expected value=1212.12
What can be wrong?
en-US seperates the position after decimal point with a . instead of a ,
try another culture or set myTextBox.Text to 1212.12
You have a missmatching culture:
1212,12
and
en-US
Either use . as a decimal separator (i.e. 1212.12) or a culture which uses , (e.g. de-DE, de-AT, fr-FR), but en-US uses . as a decimal separator and , as a thousands separator.
Either change the separator or change the culture:
For en-US culture separator is .
If you want to use separator , use fr-FR culture !
var culture = CultureInfo.CreateSpecificCulture("fr-FR");
var value = decimal.Parse(myTextBox.Text, culture);
en-US culture have NumberDecimalSeparator as a . not , but it has NumberGroupSeparator as a ,
That's why it thinks your , is NumberGroupSeparator and that's why it parses as 121212.
As a solution, you can use different IFormatProvider which has , as a NumberDecimalSeparator or you can clone your en-US culture with CultureInfo.Clone method and set it's NumberDecimalSeparator property to , and changing it NumberGroupSeparator property something else.
string s = "1212,12";
var culture = (CultureInfo)CultureInfo.CreateSpecificCulture("en-US").Clone();
culture.NumberFormat.NumberDecimalSeparator = ",";
culture.NumberFormat.NumberGroupSeparator = ".";
decimal value = decimal.Parse(s, culture); // 1212.12

Trying to set the decimal separator for the current language, getting "Instance is read Only"

I have code that was originally written for an English language market where the decimal separator is "." so it's expecting numeric values as strings to use "." as the separator. But we now have users in other places, e.g., places in Europe where the decimal separator is ",".
So, in the context of my software (really just the current thread) I want to override the decimal separator for the current language to be "." even if it defaults to something else.
I tried
String sep = ".";
NumberFormatInfo nfi1 = NumberFormatInfo.CurrentInfo;
nfi1.NumberDecimalSeparator = sep;
But I get an "Instance is read-only" exception on the third line. Apparently NumberFormatInfo is not writable. So how DO you set the current language's decimal separator to something other than its default?
You need to create a new culture and you can use the current culture as a template and only change the separator.
Then you must set the current culture to your newly created one as you cannot change the property within current culture directly.
string CultureName = Thread.CurrentThread.CurrentCulture.Name;
CultureInfo ci = new CultureInfo(CultureName);
if (ci.NumberFormat.NumberDecimalSeparator != ".")
{
// Forcing use of decimal separator for numerical values
ci.NumberFormat.NumberDecimalSeparator = ".";
Thread.CurrentThread.CurrentCulture = ci;
}
You can use the Clone() method on the NumberFormatInfo instance, which will create a mutable version (i.e. IsReadOnly = false). You are then able set the currency symbol and/or other number format options:
string sep = ".";
NumberFormatInfo nfi1 = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
nfi1.NumberDecimalSeparator = sep;

Double.TryParse converts 0.1 to 1.0

Situation - The thread culture in my web app has been set to 'es' (Spanish)
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("es");
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("es");
The string value is "0.1"
For the following expression,
var value = "0.1"
provider = CultureInfo.CreateSpecificCulture("en-US")
double.TryParse(value.ToString(), NumberStyles.Any, provider, out number)
number returns 1.0. Which makes me think that it is picking the culture info from the thread. Not the one I provide.
The following unit test passes (as expected).
var numberInEnUS = "0.1";
var spanishCulture = CultureInfo.CreateSpecificCulture("es");
culture = new CultureInfo("en-US", false);
Thread.CurrentThread.CurrentCulture = spanishCulture;
Thread.CurrentThread.CurrentUICulture = spanishCulture;
double number;
double.TryParse(numberInEnUs, NumberStyles.Any, culture, out number);
Assert.AreEqual(0.1, number);
So, the question is why does double.TryParse fail in my application? Theoretically, 0.1 for Spanish is 1 (Separator for spanish is a decimal point '.'). However, number 1000.0 does not get converted to 10000. So, it seems that it fails only for 0.1
Any explanation is highly appreciated!
You say "0.1" is number in spanish. Actually not, It is numberInEnglish or something else
var numberInSpanish = "0.1";//this is number in english culture
It should be
var numberInSpanish = "0,1";//<--Note 0,1
NumberDecimalSeparator for spanish is ,. Parse 0,1 you'll get expected result.
var numberInSpanish = "0,1";
var spanishCulture = CultureInfo.CreateSpecificCulture("es");
var culture = new CultureInfo("en-US", false);
Thread.CurrentThread.CurrentCulture = spanishCulture;
Thread.CurrentThread.CurrentUICulture = spanishCulture;
double number;
double.TryParse(numberInSpanish, NumberStyles.Any, spanishCulture, out number);
Here number is correctly parsed to "0.1"
Your problem is in the mixture of decimal and thousand separators, namely:
'.' - thousand separator in "es" culture, when parsing, will be ignored (e.g. 1.000,0 == 1000,0)
',' - decimal deparator in "es" culture, separates integer and fractional parts
You can easily convince yourself:
var spanishCulture = CultureInfo.CreateSpecificCulture("es");
Char dS = spanishCulture.NumberFormat.NumberDecimalSeparator; // <- ','
Char tS = spanishCulture.NumberFormat.NumberGroupSeparator; // <- '.'
So, in your case the string "0.1" will be converted into 1.0 double since '.' as
being a thousand separator in es culture will be ignored.
You can do either:
Use Invariant culture instead of "es" one:
double.TryParse(numberInNeutral, NumberStyles.Any, CultureInfo.InvariantCulture, out number);
Or use actual Spanish number representation:
var numberInSpanish = "0,1";
double.TryParse(numberInSpanish, NumberStyles.Any, culture, out number);
I finally was able to identify what was wrong. The issue was not with the TryParse() function but the ToString() function.
The value was actually a Double type, not a string as I mentioned above. (My bad, I thought it was not relevant). I was actually doing a value.ToString(). This is where it uses the thread culture and changes the value.
So, if the value was 0.1, the value.ToString() changes it to "0,1". It automatically changes the decimal character based on the Thread culture. The TryParse then uses the en-US culture and convert "0,1" to 1.
To fix it, use Convert.ToString instead and pass in the culture info.
At the end, it was just a silly mistake.
LessonLearnt - Be careful when using ToString() in globalized applications!

Decimal.Parse and incorrect string format error

I have a simple problem with decimal parsing. The following code works fine on my computer but when I publish the project on the server (VPS, Windows Server 2008 R2 standard edition) I get the error "Input string was in incorrect format." Any ideas what's wrong?
I store that parsed number in the MySQL DB table - the column type is DECIMAL(10, 4)
Source Code:
CultureInfo nonInvariantCulture = new CultureInfo("en-AU"); //or pl-PL
nonInvariantCulture.NumberFormat.NumberDecimalSeparator = ".";
Thread.CurrentThread.CurrentCulture = nonInvariantCulture;
string toConvert = ("3,4589").Replace(",", "."); //it's an example
decimal parsed = decimal.Parse(toConvert);
If you know that the string representation of the number uses comma as the decimal separator you can parse the value using a custom NumberFormatInfo:
var number = "3,4589";
var numberFormatInfo = new NumberFormatInfo { NumberDecimalSeparator = "," };
var value = Decimal.Parse(number, numberFormatInfo);
You can also use an existing CultureInfo for a culture that you know will work like pl-PL but I think this is easier to understand.
If on the other hand the format of the number is 3.4589 you can simply use CultureInfo.InvariantCulture which you can consider a kind of "default" culture based on en-US:
var number = "3.4589";
var value = Decimal.Parse(number, CultureInfo.InvariantCulture);
You can build a custom NumberFormatInfo to parse your value
something on these lines
NumberFormatInfo numinf = new NumberFormatInfo();
numinf.NumberDecimalSeparator= ",";
decimal.Parse("3,4589", numinf);
I guess for a work around the below code will sort it out the problem.
decimal parsed = decimal.Parse(toConvert, CultureInfo.InvariantCulture);

Double parse with culture format

I have a double number as string. The number is
202.667,40
Which is 202667.4
How can I parse this string to get the value like: Double.Parse("202.667,40",?what here), or any other method to get the value would be great. Thanks
First, you need to know which culture this number is from, then:
CultureInfo culture = new CultureInfo("de"); // I'm assuming german here.
double number = Double.Parse("202.667,40", culture);
If you want to parse using the current thread culture, which by default is the one set for the current user:
double number = Double.Parse("202.667,40", CultureInfo.CurrentCulture);
I think i have found a solution which does not require a culture. Using a NumberFormatInfo you can force a format, no matter the culture:
// This is invariant
NumberFormatInfo format = new NumberFormatInfo();
// Set the 'splitter' for thousands
format.NumberGroupSeparator = ".";
// Set the decimal seperator
format.NumberDecimalSeparator = ",";
Then later:
System.Diagnostics.Debug.WriteLine(double.Parse("202.667,40", format)));
Outputs:
202667,4
Of course, this output (inner toString()) might differ per Culture(!)
Note that changing the input to "202,667.40" will result in a parse error, so the format should match your expected input.
Hope this helps someone..
Instead of having to specify a locale in all parses, I prefer to set an application wide locale, although if string formats are not consistent across the app, this might not work.
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("pt-PT");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("pt-PT");
Defining this at the begining of your application will make all double parses expect a comma as the decimal delimiter.
You could use Double.Parse(your_number, CultureInfo.CurrentCulture) and set CurrentCulture accordingly with Thread.CurrentThread.CurrentCulture.
Example:
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-ES");
then later
Double.Parse(your_number, CultureInfo.CurrentCulture);
Note that if you explicitly assign the culture to the CurrentThread, it only applies to that thread.
var val=double.Parse( yourValue, CultureInfo.InvariantCulture);
http://www.erikschierboom.com/2014/09/01/numbers-and-culture/
For more flexibility you can set NumberDecimalSeparator
string number = "202.667,40";
double.Parse(number.Replace(".", ""), new CultureInfo(CultureInfo.CurrentCulture.Name) {NumberFormat = new NumberFormatInfo() {NumberDecimalSeparator = ","}});
Double.Parse("202.667,40", new System.Globalization.CultureInfo("de-DE"));
Instead of de-DE use whatever culture the string is in.

Categories