Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Convert String to Double with formatting

Convert String to Double with formatting

Scheduled Pinned Locked Moved Visual Basic
csharpwinformshelpquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrBean
    wrote on last edited by
    #1

    Hi, I'm developing an WinForms application in VB.NET where the user can specify custom formats for numeric data entry fields. When I display data in lists, labels, editboxes, etc. I simply use Double.Format(strCustomFormat) to format the double values in the desired format - no problem here. But I'm having a problem converting the strings back to a double since I basicly don't know if the user is using a 1.234.567,09 or 1,234,567.09 format. So basicly I need a Double.Parse method where I can specify the format (similar to the ParseExact that DateTime has)... but this is not available as standard for Doubles :( Is there any other way I can do this ? Thanks you in advance. /Bean

    A M R 3 Replies Last reply
    0
    • M MrBean

      Hi, I'm developing an WinForms application in VB.NET where the user can specify custom formats for numeric data entry fields. When I display data in lists, labels, editboxes, etc. I simply use Double.Format(strCustomFormat) to format the double values in the desired format - no problem here. But I'm having a problem converting the strings back to a double since I basicly don't know if the user is using a 1.234.567,09 or 1,234,567.09 format. So basicly I need a Double.Parse method where I can specify the format (similar to the ParseExact that DateTime has)... but this is not available as standard for Doubles :( Is there any other way I can do this ? Thanks you in advance. /Bean

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      Hi, Use string replace(",","") method for removing , commas

      M 1 Reply Last reply
      0
      • A Anonymous

        Hi, Use string replace(",","") method for removing , commas

        M Offline
        M Offline
        MrBean
        wrote on last edited by
        #3

        Please read my thread again... it's far more complicated than that; a simple "," replace/removal won't do the job. For example... does "100,123" mean 100.123 or 100123.00 ... it's impossible to tell without actually knowing the format.

        A 1 Reply Last reply
        0
        • M MrBean

          Please read my thread again... it's far more complicated than that; a simple "," replace/removal won't do the job. For example... does "100,123" mean 100.123 or 100123.00 ... it's impossible to tell without actually knowing the format.

          A Offline
          A Offline
          Anonymous
          wrote on last edited by
          #4

          This is a complicated task especially since you are giving your user the freedom of customizing the formats. Like you have said: MrBean wrote: it's impossible to tell without actually knowing the format Therefore, I would suggest giving your user a list of choices. That way you can create your Double.Parse function according to the formats that you have allowed the user the choose from. I would suggest giving your user full freedom of specifying formats is a bad idea, especially when dealing with data.

          1 Reply Last reply
          0
          • M MrBean

            Hi, I'm developing an WinForms application in VB.NET where the user can specify custom formats for numeric data entry fields. When I display data in lists, labels, editboxes, etc. I simply use Double.Format(strCustomFormat) to format the double values in the desired format - no problem here. But I'm having a problem converting the strings back to a double since I basicly don't know if the user is using a 1.234.567,09 or 1,234,567.09 format. So basicly I need a Double.Parse method where I can specify the format (similar to the ParseExact that DateTime has)... but this is not available as standard for Doubles :( Is there any other way I can do this ? Thanks you in advance. /Bean

            M Offline
            M Offline
            Mandar Patankar
            wrote on last edited by
            #5

            Hi there, I see the problem ur facing I had a similar one.You can do this little trick and I think the problem Should slove you basically have two formats here 1) 1.235.567,09 2) 1,234,567.09 To what I have understood,If format 1 is used there would be only one comma in the string and if format 2 is used there will be only single decimal So now you can parse the string and check how many decimals or commas exists in a string if decimals are more than one remove decimals and if commas are more than one remove commas. Now the question if what if there is only one comma and one decimal in the string. then Whatever comes first remove that and you should be good Hope this helps Let me know abt it Mandar Patankar Microsoft Certified professional

            M 1 Reply Last reply
            0
            • M MrBean

              Hi, I'm developing an WinForms application in VB.NET where the user can specify custom formats for numeric data entry fields. When I display data in lists, labels, editboxes, etc. I simply use Double.Format(strCustomFormat) to format the double values in the desired format - no problem here. But I'm having a problem converting the strings back to a double since I basicly don't know if the user is using a 1.234.567,09 or 1,234,567.09 format. So basicly I need a Double.Parse method where I can specify the format (similar to the ParseExact that DateTime has)... but this is not available as standard for Doubles :( Is there any other way I can do this ? Thanks you in advance. /Bean

              R Offline
              R Offline
              Rizwan Bashir
              wrote on last edited by
              #6

              hye .. your problem looks very critical. well if you have the mask for the format which user is using then it is fine like ##,##,##.00 or ##.##.##,00 then you can see which option is decimal like in first . represents decimal and in next , represents decimal. but if this is not the case and you dont have the mask then tell me what is this 12,0 $ and 12.0 $ .. you have to tell me which one is 120 $ and which one is 12 $. now you cannot even visually tell which one is what and believe me I shall go opposite to your answer :-D .So restrict your user to one mask other wise you will end up with a big money theft in company account.

              1 Reply Last reply
              0
              • M Mandar Patankar

                Hi there, I see the problem ur facing I had a similar one.You can do this little trick and I think the problem Should slove you basically have two formats here 1) 1.235.567,09 2) 1,234,567.09 To what I have understood,If format 1 is used there would be only one comma in the string and if format 2 is used there will be only single decimal So now you can parse the string and check how many decimals or commas exists in a string if decimals are more than one remove decimals and if commas are more than one remove commas. Now the question if what if there is only one comma and one decimal in the string. then Whatever comes first remove that and you should be good Hope this helps Let me know abt it Mandar Patankar Microsoft Certified professional

                M Offline
                M Offline
                MrBean
                wrote on last edited by
                #7

                My current solution (which is not 100%) works a lot like you describes : detects which comes first, "," or "." But the case where there are no decimals but thousand delimiters... example : 123,456 It impossible to know if this means 123.456 or 12345.00 without knowing the input format. I was just wondering if there was a ".NET way" of converting strings back to doubles and specifying a format string which the parser should use... just like the ParseExact which the Date type supports just fine. It seems this is a big oversight from the .NET team. Unfortunately I can't limit the formats the users can enter to a predefined set - the users expect full freedom here and the old (non-.NET) version of the app had this functionality :(

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups