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
  1. Home
  2. General Programming
  3. C#
  4. IParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString!

IParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString!

Scheduled Pinned Locked Moved C#
csharpc++question
7 Posts 5 Posters 37 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.
  • T Offline
    T Offline
    Tracy Dryden
    wrote on last edited by
    #1

    I am attempting to write a short program in C# that deals with resistances (in electronics). I would like to be able to parse string values into floating point values. The string can be in the format like 100, 4.7K, 2.2M, or shorthand equivalents like 100, 4K7, 2M2. They should never have more than 3 digits, at least one of which is a whole number part (no more than 2 decimals). Also I would like to be able to display them in similar formats. I've looked into MS documentation, and I'm more confused than ever. When do I need to use (if at all) IFormatProvider, ICustomFormatter, IFormattable, IParsable, etc. I've gone 'round and 'round, and I'm thoroughly at sea. I'm not a new programmer, just a retired one. I wrote code for about 30 years in everything from COBOL to VB.NET (including some C and C++). I don't need someone to write the code for me, but any hints would be much appreciated.

    M R J B 4 Replies Last reply
    0
    • T Tracy Dryden

      I am attempting to write a short program in C# that deals with resistances (in electronics). I would like to be able to parse string values into floating point values. The string can be in the format like 100, 4.7K, 2.2M, or shorthand equivalents like 100, 4K7, 2M2. They should never have more than 3 digits, at least one of which is a whole number part (no more than 2 decimals). Also I would like to be able to display them in similar formats. I've looked into MS documentation, and I'm more confused than ever. When do I need to use (if at all) IFormatProvider, ICustomFormatter, IFormattable, IParsable, etc. I've gone 'round and 'round, and I'm thoroughly at sea. I'm not a new programmer, just a retired one. I wrote code for about 30 years in everything from COBOL to VB.NET (including some C and C++). I don't need someone to write the code for me, but any hints would be much appreciated.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Hah been there done that one - try parsing Murex input. We had to build up a set of patterns to test the input against, it took ages to nail the bulk of the patterns that traders would enter as values and they were not limited to 3 characters. Test each entry against your pattern list, use any found pattern to parse the values, throw an error if no match is found (and add it to your pattern list). Oh and forget the existing tools as this falls way outside their design specs.

      Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

      T 1 Reply Last reply
      0
      • M Mycroft Holmes

        Hah been there done that one - try parsing Murex input. We had to build up a set of patterns to test the input against, it took ages to nail the bulk of the patterns that traders would enter as values and they were not limited to 3 characters. Test each entry against your pattern list, use any found pattern to parse the values, throw an error if no match is found (and add it to your pattern list). Oh and forget the existing tools as this falls way outside their design specs.

        Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

        T Offline
        T Offline
        Tracy Dryden
        wrote on last edited by
        #3

        You mean don't use any of those interfaces, just parse them myself? I'm already kinda of doing that, but I was hoping going the interface method might be more flexible.

        1 Reply Last reply
        0
        • T Tracy Dryden

          I am attempting to write a short program in C# that deals with resistances (in electronics). I would like to be able to parse string values into floating point values. The string can be in the format like 100, 4.7K, 2.2M, or shorthand equivalents like 100, 4K7, 2M2. They should never have more than 3 digits, at least one of which is a whole number part (no more than 2 decimals). Also I would like to be able to display them in similar formats. I've looked into MS documentation, and I'm more confused than ever. When do I need to use (if at all) IFormatProvider, ICustomFormatter, IFormattable, IParsable, etc. I've gone 'round and 'round, and I'm thoroughly at sea. I'm not a new programmer, just a retired one. I wrote code for about 30 years in everything from COBOL to VB.NET (including some C and C++). I don't need someone to write the code for me, but any hints would be much appreciated.

          R Offline
          R Offline
          Ralf Meier
          wrote on last edited by
          #4

          I would also write my own Parser because the rules for this are clear. I'm not sure if I would try against a list because there are not much points to test against : - 1st I would search for the multiplier (if there is one) - m for 0.001, k for 1000 and M for 1000000 - then I would test the value before the multiplier (if there is one) - last I would look, if you have a multiplier if there is a value after it -> now build you output-value

          T 1 Reply Last reply
          0
          • R Ralf Meier

            I would also write my own Parser because the rules for this are clear. I'm not sure if I would try against a list because there are not much points to test against : - 1st I would search for the multiplier (if there is one) - m for 0.001, k for 1000 and M for 1000000 - then I would test the value before the multiplier (if there is one) - last I would look, if you have a multiplier if there is a value after it -> now build you output-value

            T Offline
            T Offline
            Tracy Dryden
            wrote on last edited by
            #5

            That's pretty much the way I am doing it now.

            1 Reply Last reply
            0
            • T Tracy Dryden

              I am attempting to write a short program in C# that deals with resistances (in electronics). I would like to be able to parse string values into floating point values. The string can be in the format like 100, 4.7K, 2.2M, or shorthand equivalents like 100, 4K7, 2M2. They should never have more than 3 digits, at least one of which is a whole number part (no more than 2 decimals). Also I would like to be able to display them in similar formats. I've looked into MS documentation, and I'm more confused than ever. When do I need to use (if at all) IFormatProvider, ICustomFormatter, IFormattable, IParsable, etc. I've gone 'round and 'round, and I'm thoroughly at sea. I'm not a new programmer, just a retired one. I wrote code for about 30 years in everything from COBOL to VB.NET (including some C and C++). I don't need someone to write the code for me, but any hints would be much appreciated.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Tracy Dryden wrote:

              Also I would like to be able to display them in similar formats.

              Two ways to do that after you have parsed. First keep the original value as a string. You can create a class that has both the parsed value and the original value. Second create your own format method(s) that take the value and normalize the display. Problem with this is you will need to deal with precision.

              1 Reply Last reply
              0
              • T Tracy Dryden

                I am attempting to write a short program in C# that deals with resistances (in electronics). I would like to be able to parse string values into floating point values. The string can be in the format like 100, 4.7K, 2.2M, or shorthand equivalents like 100, 4K7, 2M2. They should never have more than 3 digits, at least one of which is a whole number part (no more than 2 decimals). Also I would like to be able to display them in similar formats. I've looked into MS documentation, and I'm more confused than ever. When do I need to use (if at all) IFormatProvider, ICustomFormatter, IFormattable, IParsable, etc. I've gone 'round and 'round, and I'm thoroughly at sea. I'm not a new programmer, just a retired one. I wrote code for about 30 years in everything from COBOL to VB.NET (including some C and C++). I don't need someone to write the code for me, but any hints would be much appreciated.

                B Offline
                B Offline
                Bosse62
                wrote on last edited by
                #7

                I would use switch statements, multiple switch statements inside switch statements! Sounds complicated? Bare with me. Start with a a TryParse to find out if your input contains numbers-only or not. (not digits-only) You might need to handle different decimal signs, period or comma. Now you can call two different switches: Inside the numbers-only switch there probably wouldn't be many 'case' statements. Use TryParse or ConvertTo to get a Double. Examine the value you got so it is within reason. Convert the Double to a suitable integer or keep it as is depending on what you need. Inside the digits+char switch you may use String.Contains('some char') to branch out one of these: 1) More switches depending on what character was found - Maybe branch even further depending on what position the character was found... (maybe not, but you get what I'm saying?) 2) Different methods (with good names explaining what they do) Inside some of these methods it might be useful to use even more switches for readability purposes. Then you start using TryParse/ConvertTo as I descibed above. What I'm saying is: One step at a time, Keep It Simple, and branch out to more specifics on the way. Using Interfaces or RegEx or whatever to "parse" your data in "one" line... and you will end up in some completely unreadable and non-maintainable code. Best regards The K.I.S.-guy

                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