IParsable? IFormattable? ICustomFormatter? IFormatProvider? TryParse! ToString!
-
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.
-
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.
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
-
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
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.
-
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.
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
-
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
That's pretty much the way I am doing it now.
-
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.
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.
-
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.
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