Decimal formatting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robertybob
    New Member
    • Feb 2013
    • 116

    Decimal formatting

    Hi

    I have an application that could be used all around the world. Obviously different cultures have different methods of writing decimals.

    I am importing System.Globalis ation but it seems if the code has something like Convert.ToDecim al("1.2") it fails if the user's machine is set up to, say, a comma format.

    I'm not sure why since I'd have thought it would convert to whatever the user's machine thinks a decimal is.

    How do I deal with this in the most effective way?

    Should I use Double? Does that eliminate any cultural problems? Or do I need a different way to force an input to a decimal?

    Thanks
  • gabemr
    New Member
    • Jan 2015
    • 3

    #2
    Hey there,

    Have you tried using the CultureInfo.Cur rentCulture property? Can you write some code with the current problem as an example to see if I can help you better?

    Here is a link that might help: http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx

    Comment

    • robertybob
      New Member
      • Feb 2013
      • 116

      #3
      Hi Gabe,

      Thanks for the reply.

      From research here, I think I'm agreed that forcing the culture on every thread is the way forward.

      However I cannot seem to get the code used in the link provided to be accepted by Visual Studio - maybe I'm missing something or I'm not declaring something.

      So, say I have...

      Code:
      Imports System.Globalization
      Imports System.Threading
      
      Public Class My_Form
      Private clientname As String = ""
      etc....
      
      End Class
      Where do I add the culture info to force the entire form to "en-GB" - and what code is required?

      Many thanks!

      Comment

      • robertybob
        New Member
        • Feb 2013
        • 116

        #4
        I notice also that the MSDN page says to insert code in the My_Form.Designe r before the InitializeCompo nents but, again, if I try to paste the suggested code in there Visual Studio tells me it isn't compatible.

        Thanks

        Comment

        • robertybob
          New Member
          • Feb 2013
          • 116

          #5
          This might be a stupid observation, but would simply changing the Language on the form to English(United Kingdom) force the culture on the form to en-GB for all users irrespective of their machine settings?

          Comment

          • robertybob
            New Member
            • Feb 2013
            • 116

            #6
            Ok - seems the answer to the above is 'no' so I'm back to trying to force the culture :)

            Comment

            • gabemr
              New Member
              • Jan 2015
              • 3

              #7
              Hey,

              Sorry I couldn't get to your replies on time. You could try changing the current culture on a form load event.

              For example:

              Code:
              Imports System.Globalization
              Imports System.Threading
              Public Class Form1
                  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                      Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB")
                  End Sub
              End Class
              Okay so now, lets say this form has a button, a textbox, and a label that will show the output of an operation. When the user enters a number in the textbox, the label's text will change to the provided number in a decimal format and the current culture.

              Code example:

              Code:
              Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
              
                      Dim textEntry As Decimal = TextBox1.Text()
                      textEntry = Convert.ToDecimal(textEntry)
              
                      Label1.Text = textEntry.ToString("C", CultureInfo.CurrentCulture)
                  End Sub
              Hope this helps

              Comment

              • robertybob
                New Member
                • Feb 2013
                • 116

                #8
                Thanks Gabe.

                At the moment this seems to be eliminating the crashes so seems ok. Was a simpler solution than expected from reading the MSDN docs.

                I have read somewhere that this will need adding to any threads opened by the main thread so will add there too. Not sure about backgroundWorke r threads.

                Thanks for the assistance!

                Comment

                Working...