I am working with some old data imports and came across a bunch of data from an external source that reports financial numbers with a signed overpunch. I\'ve seen alot, but
If you don't have enough already here is another option using an extension method, you could make this better by using some of the ideas in the other posts.
///
/// Extension method to get overpunch value
///
/// the text to convert
/// int
public static int OverpunchValue(this String number)
{
int returnValue;
var ovpValue = OverPunchValues.Instance.OverPunchValueCollection.First(o => o.OverpunchCharacter ==
Convert.ToChar(number.Substring(number.Length - 1)));
returnValue = Convert.ToInt32(number.Substring(0, number.Length - 1) + ovpValue.NumericalValue.ToString());
return ovpValue.IsNegative ? returnValue * -1 : returnValue;
}
/*singleton to store values */
public class OverPunchValues
{
public List OverPunchValueCollection { get; set; }
private OverPunchValues()
{
OverPunchValueCollection = new List();
OverPunchValueCollection.Add(new OverPunchValue { OverpunchCharacter = '{', IsNegative = true, NumericalValue = 0 });
OverPunchValueCollection.Add(new OverPunchValue { OverpunchCharacter = 'J', IsNegative = true, NumericalValue = 1 });
//add the rest of the values here...
}
static readonly OverPunchValues _instance = new OverPunchValues();
public static OverPunchValues Instance
{
get { return _instance; }
}
}
public class OverPunchValue
{
public char OverpunchCharacter { get; set; }
public bool IsNegative { get; set; }
public int NumericalValue { get; set; }
public OverPunchValue()
{
}
}
And then you can call it like:
string str = "00345{";
int temp = str.OverpunchValue();