473,406 Members | 2,371 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,406 software developers and data experts.

Decimal formatting

116 64KB
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

7 1494
gabemr
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
robertybob
116 64KB
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
robertybob
116 64KB
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
robertybob
116 64KB
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
robertybob
116 64KB
Ok - seems the answer to the above is 'no' so I'm back to trying to force the culture :)
Jan 15 '15 #6
gabemr
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
robertybob
116 64KB
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

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

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
4
by: Tommi Mäkitalo | last post by:
Hi I need to format floating-point-numbers with exact 2 digits after decimal point. I could use printf with "%.2f", but it don't use std::locale. Any ideas? -- Tommi Mäkitalo
5
by: Jeff Cook | last post by:
Hi I have a DataGrid that I am using to display a .xml file (that has a schema in a .xsd), like this:- ds.ReadXmlSchema(sDataPath + "Data.xsd"); ds.ReadXml(sDataPath + "Data.xml",...
5
by: Paperback Writer | last post by:
Hi, i have in my application a field that is percentual, that means...can have comma...my property that receive this data is a decimal and in my SQLServer is also a decimal, but when i type, for...
14
by: Scott M. | last post by:
Ok, this is driving me nuts... I am using VS.NET 2003 and trying to take an item out of a row in a loosely-typed dataset and place it in a label as a currency. As it is now, I am getting my...
5
by: Stefantastisk | last post by:
Hey there, Anyone knows a clever method for knowing the length of the digit after a decimal point in a standard C# decimal value, WITHOUT use of any string formatting. Example: 5231,12231 <-...
3
by: Harlin Seritt | last post by:
Cant believe I have to post this and get help... and can't believe I couldnt Google it reasonably well enough... I need to take a float variable and have it display as a string to always have two...
2
by: lawpoop | last post by:
Hello all -- I'm trying to format decimal numbers for a report. I would have to have the number with leading zeros to the tens' place, and trailing zeros to the thousandths' place. I'm using...
2
Pittaman
by: Pittaman | last post by:
Hello I am creating some crystal reports (for visual studio 2005) based on the content of certain .NET objects. I'm doing this in .NET 2.0. For one of them I'm using a Cross-table to summarize...
1
by: AccessInCA | last post by:
I have a report that is grouped by a field called DataSource. In the detail section of the report where the record data fields are displayed, such as Jan, Feb, Mar, etc, I want to format the decimal...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.