Results 1 to 2 of 2

Thread: [RESOLVED] ToDouble and locales

  1. #1

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Resolved [RESOLVED] ToDouble and locales

    Hi,

    So I have this string to double conversion in my code and I noticed the conversion uses a comma for decimal notation.
    Now I know that in some locales the decimal notation is with a period.
    Does this affect my code?
    If so, how could I adapt the code?

    Thanks.
    Delete it. They just clutter threads anyway.

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

    Re: ToDouble and locales

    Converting strings to numbers and vice versa will use the current culture by default. That way, things work for everyone the way they expect. For instance, if a user has their computer set to use a comma as a decimal separator and they enter "1,2" then Convert.ToDouble will convert that successfully, while another user who has their computer set to use a period as the decimal separator and they enter "1.2" then Convert.ToDouble will convert that successfully to the same number.

    It's only when you expect formats other than the system defaults to be used that you have issues. For instance, if the user has their computer set to use a comma as the decimal separator and then you feed the app a CSV file that contains numbers that use periods as the decimal separator, then you'll have issues. If you know that the data will be in a specific format regardless of the system defaults, then you should hard-code that format. If you expect the data to conform to the system defaults then you should accept the system defaults. If you want to let the user choose the format at run-time then you need to build that functionality into your app. The root of this stuff is the CultureInfo class and its properties, including NumberFormat, which refers to a NumberFormatInfo object.
    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

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