473,473 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Decimal.TryParse failed in da-DK culture.

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
1 6840
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Blake T. Garretson | last post by:
I'm having some issues with decimal.Decimal objects playing nice with custom data types. I have my own matrix and rational classes which implement __add__ and __radd__. They know what to do with...
2
by: News charter | last post by:
I have a form that I need to pass the sales tax. Works great if sales tax is an interger. When I try to parse the sales tax with decimal place with Int32.Parse method I get exception. The...
7
by: Paul K | last post by:
I'm writing a small component that needs to be as fast as possible. The component needs to convert a string to decimal during the course of it's processing. However, I need to test the string...
5
by: bas jaburg | last post by:
Hi, I need to know the regular expression that checks the whether a 1-decimal number has been entered. Also it needs to ok if no trailing number before the decimal dot has been entered So:...
3
by: Artek | last post by:
Hello, after installing .net 1.1, ASP.NET works fine, but there is a problems with extra zeros added to the numbers at the end. In Sql 2000 SP3a there is for example a table with one column ...
0
by: Martyn Fewtrell | last post by:
Hi there I'm sure there is a straight forward answer to this but I haven't quite got there. If I have a String in Sterling Currency Format and try to convert this to a Decimal (for arithmetic...
1
by: VB Programmer | last post by:
I am getting a "IErrorInfo.GetDescription failed with E_FAIL" error when trying to do a "Fill" on a dataadapter. Any ideas?? The SQL command seems good (to an Access db). Here is the general...
3
by: Agnes | last post by:
What value will be return Decimal.parse(Me.txtTest.text) ???? It seems doesn't return False or True, so. what will be returned ? Thanks
1
by: Rico | last post by:
I have tried to access a database using asp.net. after some entries like 120+, I got an unspecified error message. Anyone know what is happening, it seems to be stuck at the same line even when i...
3
by: Dave Griffiths | last post by:
Hi All Using VB 2005 I am writing a program that will be used on machines which have different locales (English, Norwegian and Swedish) I am restricting textbox input using keypress to just...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.