By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,041 Members | 1,793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,041 IT Pros & Developers. It's quick & easy.

Decimal formatting

100+
P: 116
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.Globalisation but it seems if the code has something like Convert.ToDecimal("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
Jan 13 '15 #1

✓ answered by gabemr

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:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Globalization
  2. Imports System.Threading
  3. Public Class Form1
  4.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  5.         Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB")
  6.     End Sub
  7. End Class
  8.  
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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.  
  3.         Dim textEntry As Decimal = TextBox1.Text()
  4.         textEntry = Convert.ToDecimal(textEntry)
  5.  
  6.         Label1.Text = textEntry.ToString("C", CultureInfo.CurrentCulture)
  7.     End Sub
Hope this helps

Share this Question
Share on Google+
7 Replies


P: 3
Hey there,

Have you tried using the CultureInfo.CurrentCulture 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
Jan 15 '15 #2

100+
P: 116
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...

Expand|Select|Wrap|Line Numbers
  1. Imports System.Globalization
  2. Imports System.Threading
  3.  
  4. Public Class My_Form
  5. Private clientname As String = ""
  6. etc....
  7.  
  8. End Class
  9.  
Where do I add the culture info to force the entire form to "en-GB" - and what code is required?

Many thanks!
Jan 15 '15 #3

100+
P: 116
I notice also that the MSDN page says to insert code in the My_Form.Designer before the InitializeComponents but, again, if I try to paste the suggested code in there Visual Studio tells me it isn't compatible.

Thanks
Jan 15 '15 #4

100+
P: 116
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?
Jan 15 '15 #5

100+
P: 116
Ok - seems the answer to the above is 'no' so I'm back to trying to force the culture :)
Jan 15 '15 #6

P: 3
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:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Globalization
  2. Imports System.Threading
  3. Public Class Form1
  4.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  5.         Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB")
  6.     End Sub
  7. End Class
  8.  
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:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.  
  3.         Dim textEntry As Decimal = TextBox1.Text()
  4.         textEntry = Convert.ToDecimal(textEntry)
  5.  
  6.         Label1.Text = textEntry.ToString("C", CultureInfo.CurrentCulture)
  7.     End Sub
Hope this helps
Jan 15 '15 #7

100+
P: 116
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 backgroundWorker threads.

Thanks for the assistance!
Jan 16 '15 #8

Post your reply

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