By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,120 Members | 1,786 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,120 IT Pros & Developers. It's quick & easy.

Decimal.TryParse failed in da-DK culture.

P: n/a
I have this code that always return zero whenever i passed 1,000,00 in
a da-DK culture or en-US culture. It works with amount less that 1000
like 99,99. but always returns 0 when greater than or equal to 1000.
Decimal separator is ",". I suspect its because the decimal and
thousand separator are the same.

Thread.CurrentThread.CurrentCulture = new
CultureInfo(Thread.CurrentThread.CurrentCulture.Na me);
Thread.CurrentThread.CurrentCulture.NumberFormat.C urrencySymbol = "";
Thread.CurrentThread.CurrentCulture.NumberFormat.C urrencyDecimalSeparator
= ",";
Thread.CurrentThread.CurrentCulture.NumberFormat.C urrencyGroupSeparator
= ",";
Thread.CurrentThread.CurrentCulture.NumberFormat.C urrencyDecimalDigits
= 2;

Thread.CurrentThread.CurrentUICulture = new
CultureInfo(Thread.CurrentThread.CurrentUICulture. Name);
Thread.CurrentThread.CurrentUICulture.NumberFormat .CurrencySymbol =
"";
Thread.CurrentThread.CurrentUICulture.NumberFormat .CurrencyDecimalSeparator
= ",";
Thread.CurrentThread.CurrentUICulture.NumberFormat .CurrencyGroupSeparator
= ",";
Thread.CurrentThread.CurrentUICulture.NumberFormat .CurrencyDecimalDigits
= 2;
/// <summary>
/// Try parse string input into decimal value.
/// </summary>
/// <param name="inputValue">The input value.</param>
/// <param name="valueWhenNull">The value when null or failed to
parse.</param>
/// <returns></returns>
public static decimal TryParseDecimal(string inputValue, string
valueWhenNull) {
inputValue = inputValue ?? valueWhenNull;

decimal returnedValue = 0;
decimal.TryParse(inputValue, NumberStyles.Currency,
NumberFormatInfo.CurrentInfo, out returnedValue);

return returnedValue;
}

Feb 27 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
de******@gmail.com <de******@gmail.comwrote:
I have this code that always return zero whenever i passed 1,000,00 in
a da-DK culture or en-US culture. It works with amount less that 1000
like 99,99. but always returns 0 when greater than or equal to 1000.
Decimal separator is ",". I suspect its because the decimal and
thousand separator are the same.
Are you *specifically* setting the decimal and group separator to the
same thing in your real code? A simple test program I wrote showed that
in da-DK, CurrencyDecimalSeparator is "," and CurrencyGroupSeparator is
".".

If you're setting them to the same thing programmatically, that's a
really bad idea, and will indeed have the kind of effect you're seeing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 27 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.