|
-
Jun 16th, 2010, 09:09 PM
#1
Thread Starter
Lively Member
number format, having problems with computers in different langauges.
So currently i use
Code:
Label44.Text = Format(Convert.ToDouble(Label44.Text), "#,###,###")
to convert numbers in labels to the correct format. I had a bunch of friends test the software and the ones that were having problems were from Europe where , and . are placed different "i think". And when they run the program they get a error saying something about the todouble line (can't get exact error it was in french.)
Could they be experiencing the error because their windows are in a different language setting which has different place values for "," and "."? is there a way to fix this so everyone can use my program ?
-
Jun 16th, 2010, 09:14 PM
#2
Re: number format, having problems with computers in different langauges.
Why do you need to get a number from a Label at all? If you've got text in a Label then it must have come from somewhere in the first place, presumably a number. Why aren't you just using that number?
-
Jun 17th, 2010, 02:25 AM
#3
Thread Starter
Lively Member
Re: number format, having problems with computers in different langauges.
My program actually has the user input a specific number, and then it crunches some math with it thus displaying the final result in a label text.
-
Jun 17th, 2010, 02:32 AM
#4
Re: number format, having problems with computers in different langauges.
 Originally Posted by swingyswongy
My program actually has the user input a specific number, and then it crunches some math with it thus displaying the final result in a label text.
The user obviously doesn't enter anything into a Label, so whatever is in the Label must have been put there by your code. Why can't you use the value you originally put, which was presumably a number, there instead of getting the text back from the Label? If you had a number in the first place, why do you need to get the text from the Label and convert when at best you'll end up with the same number?
I think you need to provide more details of exactly what you're trying to achieve, exactly how you're trying to achieve it and exactly what happens when you try it. If you're doing everything correctly then format should never be an issue because your code will simply assume that every input and output is in the default format for the current system. If that's not happening then it's because you're doing something wrong but we can't say what because we don't know what you're doing.
-
Jun 17th, 2010, 02:38 AM
#5
Thread Starter
Lively Member
Re: number format, having problems with computers in different langauges.
Ok let me explain exactly what its doing
There is 1 text box, 1 button, and 1 label.
User enters 150,000 or 150000 into into textbox1 and hits submit (button). Label1.text then takes textbox1.text * 10 for example and displays the result (in label1.text). The number is then converted using the doubleto code. I am using the doubleto line to format the end result since the end result is not formated correctly (label doesn't add ","s automatically)
However users in Europe (windows in a different language and currency setting) are getting a a doubletoerror when they hit the submit button. I'm assuming its because Europe uses "," and "." in different means than americans. For American and windows users that have their language set to English there is no problem what so ever there is just a problem with the language clashing. Is there another way to format the label?
Do you see whats going on now?
Thanks for your help
-
Jun 17th, 2010, 03:08 AM
#6
Re: number format, having problems with computers in different langauges.
So, you get a string from the TextBox, convert it to a number, perform some arithmetic on it to get another number, convert that to a string and display it in a Label, then get that string back from the Label and convert it to a number, then convert that number to a string and display it back in the Label again. What is the point of putting it into the Label, getting it out and putting it back again? Why not just format the number properly when you put it into the Label the first time?
Now, I'm assuming that all your users are going to enter the number into the TextBox correctly in the first place, i.e. if they use a group separator then they will use the correct one for there culture settings. It's then simply a matter of reading the input, converting it, processing it and displaying it.
vb.net Code:
Dim number As Integer If Integer.TryParse(Me.TextBox1.Text, _ Globalization.NumberStyles.AllowThousands, _ Nothing, _ number) Then number = number * 10 Me.Label1.Text = number.ToString("#,#") Else MessageBox.Show("Invalid input") End If
That is all you need. That will accept any valid integer, with or without group separators, and it will display the result with group separators, always using the correct group separator for the current culture.
-
Jun 17th, 2010, 04:02 AM
#7
Thread Starter
Lively Member
Re: number format, having problems with computers in different langauges.
yeah the i see what your talking about with the extra code (im still learning :/). Anyway i tried that above in a little program and it worked! i changed my language to french to test and didn't have any problems. So tell me if i got this right
before
Code:
Label30.Text = TextBox2.Text / hand1.Text
Label30.Text = Format(Convert.ToDouble(Label30.Text), "#,###,###")
Label34.Text = TextBox2.Text / hand2.Text
Label34.Text = Format(Convert.ToDouble(Label34.Text), "#,###,###")
Label35.Text = TextBox2.Text / hand3.Text
Label35.Text = Format(Convert.ToDouble(Label35.Text), "#,###,###")
new
Code:
Dim number As Integer
Label30.Text = TextBox2.Text / hand1.Text
number = label30.text
Me.Label30.Text = number.ToString("#,#")
Label34.Text = TextBox2.Text / hand2.Text
number = label30.text
Me.Label34.Text = number.ToString("#,#")
Label35.Text = TextBox2.Text / hand3.Text
number = label30.text
Me.Label35.Text = number.ToString("#,#")
would that be correct? do i need the Integer.TryParse(Me.TextBox1.Text, Globalization.NumberStyles.AllowThousands, Nothing, number) line?
Please excuse the probably unorthodox coding :P
-
Jun 17th, 2010, 04:10 AM
#8
Thread Starter
Lively Member
Re: number format, having problems with computers in different langauges.
well i guess if i was doing it that way ^^ i wouldn't need to dim number as an integer since its just an extra line.
Question though, with that format it works beautiful, but it rounds all numbers for example 1,500.01 is 1,501. Any way to get the exact value?
jmcilhinney 4 mod!
-
Jun 17th, 2010, 04:27 AM
#9
Re: number format, having problems with computers in different langauges.
There are a lot of problems with your code as it is.
First of all, strings are not numbers. This line:
Code:
Label30.Text = TextBox2.Text / hand1.Text
Doesn't really mean anything. It is semantically equivalent to asking you to literally divide an apple by an orange. If you want to parse some text as a number, then you need to do that explicitly:
number = Integer.Parse(TextBox2.Text) / Integer.Parse(hand1.Text)
Even so, this will crash if the text in the textbox is not a number. You can look into Integer.TryParse for some validation.
Also, if you are doing division, then Integers really aren't the best option. You have to understand that in programming, 4/5 will not return 0.8. It will return 0. This is because of the way that computers interpret and process different types of numbers. Think of the computer as totally literal. When you supply it with two Integers and do 4/5, you are asking the computer:
"How many times does 5 go into 4?"
And of course, the computer will say,
"Zero times. 5 does not go into 4."
Even if you use numbers that can be divided, like 5/4, the computer will only return the literal answer (in the last case, 1). To do full featured division with support for fractional values, you should use the Double or Decimal value type.
The code as you have it above might be better written like this:
Code:
Dim numerator As Double = Double.Parse(TextBox2.Text)
Label30.Text = (numerator/Double.Parse(hand1.Text)).ToString("#,#")
Label34.Text = (numerator/Double.Parse(hand2.Text)).ToString("#,#")
Label35.Text = (numerator/Double.Parse(hand3.Text)).ToString("#,#")
Oh and you really might want to be more descriptive with your control names.
-
Jun 17th, 2010, 05:54 AM
#10
Thread Starter
Lively Member
Re: number format, having problems with computers in different langauges.
is there just a way to make the form or entire program run in English (US) regional setting? Would make the problem alot easier since it works fine on US regional settings
-
Jun 17th, 2010, 06:41 AM
#11
Re: number format, having problems with computers in different langauges.
If you want to use non-integer numbers then use type Double instead of Integer.
In VB (4 / 5) actually is 0.8 because VB does floating-point division by default. Unlike C#, VB has an explicit integer division operator.
-
Jun 22nd, 2010, 04:08 PM
#12
Re: number format, having problems with computers in different langauges.
That's just molly coddling.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|