473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Parse a textBox to Double not working properly. Very strange.

Hello,
I am incredibly puzzled.
I have made a small, fairly simple calculator for specific purposes. I can run this fine on my computer. But when I try to run it on a target computer (from Norway) I get an error that puzzles me.

Here's the strange behavoir:

Expand|Select|Wrap|Line Numbers
  1. textBox.Text = "1";
  2. double result = double.Parse(textBox.Text)  * .15;
This piece of code works fine on both computers, and with out a dought should.

This is where it get's wierd:
Expand|Select|Wrap|Line Numbers
  1. textBox.Text = "1.1";
  2. double result = double.Parse(textBox.Text)  * .15;
This doesn't work on a computer in Norway, but on all the computers I have tried that I can access it works fine.

So the problem is converting a String to a Double.
More specifcaly this piece of code:
Expand|Select|Wrap|Line Numbers
  1. double.Parse(textBox.Text)
When
Expand|Select|Wrap|Line Numbers
  1. textBox.Text = "1.1";
Even though there should be no problem.

I have coded the calculator to where you can only enter valid characters to the textBox. (numbers and one decimal are allowed)

When I get the error from the computer in Norway, it's telling me that I can't convert "1.1" in the textBox to a Double. Which should work, but for some odd reason doesn't. I've tried a lot of differant things like taking the "1.1" from the textBox and putting it in a string, then trying to parse that, same error. So what I'm wonering is if there might be some type of language conversion problem, or something buggy with that computer?

Both computers have Win7, and the latest .NET framework. Which reminds me that I have used the 4.0 Beta framework to program this, so what I also tried was re-writing for 3.5. Which also gave me the same error.
Mar 31 '10 #1
6 6974
tlhintoq
3,525 Expert 2GB
The decimal marker in Norway is a comma, not a period.

1,1
Mar 31 '10 #2
Thanks, I'll give it a try!
Mar 31 '10 #3
Plater
7,872 Expert 4TB
Yes, these types of issues are almost always related to a difference in language/locale. Different settings use different characters as decimal seperators, thousands seperators, and etc.
If your application is going to be used globally, you will need to do a lot more research into locales :-(
Apr 5 '10 #4
Thank you so much "tlhintoq" and thanks to the asker of this question too. This problem was puzzling me for a week, and I scoured Google for how to convert a String to a Double, every time I write Double.Parse(text) it does not work when I run a preview of my Software. My OS is English, but I am developing my Software in a virtual French OS and when I type a period (.) as a decimal it bugs and gives me error. Darn it, a week of mind puzzle, and I even paused my Software for over 2 weeks. Thank you again for your valuable help. I am relocating to my English OS :)
Oct 18 '10 #5
enty
1
For example you can use this

Expand|Select|Wrap|Line Numbers
  1. string numSeparator = 
  2. CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
  3. char numSepFromThis = (char)0;
  4. char numSepToThis = (char)0;
  5. if (numSeparator == ".") 
  6. {
  7.      numSepFromThis = ',';
  8.      numSepToThis = '.';
  9. }
  10. else if (numSeparator == ",")
  11. {
  12.      numSepFromThis = '.';
  13.      numSepToThis = ',';
  14. }
to text

Expand|Select|Wrap|Line Numbers
  1. textBox.Text.Trim().Replace(numSepFromThis,numSepToThis);
Feb 10 '12 #6
GaryTexmo
1,501 Expert 1GB
Wow, that's a huge thread necro :D Thanks for posting the tip.

Also, it's worthwhile pointing out that the Parse method (as well as TryParse, I believe) will accept a CultureInfo object. For example...

Expand|Select|Wrap|Line Numbers
  1.             double d1 = double.Parse("3.14", Thread.CurrentThread.CurrentCulture);
  2.             double d2 = double.Parse("3,14", new CultureInfo("nb-NO"));
  3.  
  4.             Console.WriteLine(d1.ToString());
  5.             Console.WriteLine(d2.ToString());
Your code would need to use the System.Globalization and System.Threading namespaces to access this code. The first one will parse "3.14" with whatever the current thread's culture is. In my case it would be "en-US". The second parses with the Norwegian culture and correctly parses "3,14" to a float value of 3.14.

Hopefully that helps someone out there!
Feb 10 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Steven | last post by:
I got a "Parse error: parse error in ..." in this line: if(empty($_POST){ ..... But if I fist assign $ssn=$_POST; and then if(empty($ssn){ ... it is working. Any advice? Thanks in advance.
1
by: Patrick De Ridder | last post by:
When I code Double.Parse(textBox1.Text) I get an error How can I convert a text box entry to a numeric? Please give a code example, if you know the answer.
21
by: William Stacey [MVP] | last post by:
Anyone know of some library that will parse files like following: options { directory "/etc"; allow-query { any; }; // This is the default recursion no; listen-on { 192.168.0.225;...
12
by: Brett Hofer | last post by:
I must be missing something - Im a veteran C++ programmer now working with C# overall I like the language but find many weird changes... Anyway Im writing code behind an aspx. In this one C#...
3
by: c0uch | last post by:
the first and third methods are in a usercontrol object. txtValue is a TextBox the float.parse in the if statement on line 3 always works fine. the second float.parse in the third method is...
5
by: Markus Kling | last post by:
"double.Parse(double.MaxValue.ToString())" yields the following Exception: Value was either too large or too small for a Double. at System.Number.ParseDouble(String value, NumberStyles options,...
3
by: Josh | last post by:
I am writing a program where the user inputs currency in US dollars. I want the program to only accept valid currency input, converting the string into the proper type of variable (double?), and...
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
1
by: schaf | last post by:
Hi NG! I have a little question about the localization of a double. If I use the CultureInfo "en-US" I have the following behavior: 1.) If I enter the value 2.5 into a textbox the...
2
by: Lawrence Krubner | last post by:
Imagine a template system that works by getting a file, as a string, and then putting it through eval(), something like this: $formAsString = $controller->command("readFileAndReturnString",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.