Results 1 to 12 of 12

Thread: number format, having problems with computers in different langauges.

  1. #1

    Thread Starter
    Lively Member swingyswongy's Avatar
    Join Date
    Jun 2010
    Location
    Bay area, CA
    Posts
    83

    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 ?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member swingyswongy's Avatar
    Join Date
    Jun 2010
    Location
    Bay area, CA
    Posts
    83

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: number format, having problems with computers in different langauges.

    Quote Originally Posted by swingyswongy View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member swingyswongy's Avatar
    Join Date
    Jun 2010
    Location
    Bay area, CA
    Posts
    83

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim number As Integer
    2.  
    3. If Integer.TryParse(Me.TextBox1.Text, _
    4.                     Globalization.NumberStyles.AllowThousands, _
    5.                     Nothing, _
    6.                     number) Then
    7.     number = number * 10
    8.     Me.Label1.Text = number.ToString("#,#")
    9. Else
    10.     MessageBox.Show("Invalid input")
    11. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member swingyswongy's Avatar
    Join Date
    Jun 2010
    Location
    Bay area, CA
    Posts
    83

    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

  8. #8

    Thread Starter
    Lively Member swingyswongy's Avatar
    Join Date
    Jun 2010
    Location
    Bay area, CA
    Posts
    83

    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!

  9. #9
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    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.

  10. #10

    Thread Starter
    Lively Member swingyswongy's Avatar
    Join Date
    Jun 2010
    Location
    Bay area, CA
    Posts
    83

    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

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    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
  •  



Click Here to Expand Forum to Full Width