S
Spoon
So to start what I'm trying to do is use "enhance" the standard format string
options to allow" ,.~" to do the same as ",." without rounding the results.
So maybe there's something really easy I missed.
So...What I'm doing since the formatting is encapsulated way in a report
generation tool I've created a new method that will be called to format
Numbers only. The full function is below.
The problem line is
val = val/a;
Reason being I don't want to modify the actual val type but with it being a
ValueType I also can't just do the division either. Was trying a generic
function but end up with the same problem (new to generics in .NET) and I
tried casting it to a double but that's not really preferable and it didn't
like it anyway.
Anyone have any ideas?
thx
protected static string FormatNumeric(ValueType val, string format)
{
// If we have a null value then return an empty string
if (null == val)
{ return string.Empty; }
// If we have no format string then just return ToString on the
val
if (string.IsNullOrEmpty(format))
{ return val.ToString(); }
// Split into the 3 possible format forms
// posative #s;negative #s;zeros
string[] formats = format.Split(';');
// For comparisons against 0 convert ValueType to a double as it
is the
// most precise of the numerical types so we won't inaccuratly
compare the
// value this way.
System.Reflection.MethodInfo compareMethod =
val.GetType().GetMethod("CompareTo", new Type[] { val.GetType() });
if (null == compareMethod)
{
// throw ex;
}
int rVal = (int)compareMethod.Invoke(val, new object[] { 0 });
int formatIdx = -1;
if (0 < rVal && 1 <= format.Length) // Number is posative use
format 1
{ formatIdx = 0; }
else if (0 > rVal && 2 <= format.Length) // Number is negative
use format 2
{ formatIdx = 1; }
else if (0 == rVal && 3 <= format.Length) // Number is equal to
0 use format 3
{ formatIdx = 2; }
// If we have a posative value
if (-1 < formatIdx)
{
// Simple regex expression. Any # of ,'s followed by a
period(.) and then a tilda(~)
string reg = ",+\\.~";
Regex regx = new Regex(reg);
Match match = regx.Match(formats[formatIdx]);
if (match.Success)
{
// Now figure out how many ,'s exist before the . and
adjust
// the 1000's divide value accordingly. (each comma
represents a thousnds power)
int idx = match.Value.IndexOf('.') + 1;
long a = (int)System.Math.Pow(1000, idx);
System.Reflection.MethodInfo divide =
val.GetType().GetMethod("operator /", new Type[] { typeof(int) });
val = val/a;
format = Regex.Replace(format, reg, string.Empty);
}
}
System.Reflection.MethodInfo tostringMethod =
val.GetType().GetMethod("ToString", new Type[] {typeof(string)});
if (null == tostringMethod)
{
// throw ex;
}
return (string)tostringMethod.Invoke(val, new object[] { format
});
}
options to allow" ,.~" to do the same as ",." without rounding the results.
So maybe there's something really easy I missed.
So...What I'm doing since the formatting is encapsulated way in a report
generation tool I've created a new method that will be called to format
Numbers only. The full function is below.
The problem line is
val = val/a;
Reason being I don't want to modify the actual val type but with it being a
ValueType I also can't just do the division either. Was trying a generic
function but end up with the same problem (new to generics in .NET) and I
tried casting it to a double but that's not really preferable and it didn't
like it anyway.
Anyone have any ideas?
thx
protected static string FormatNumeric(ValueType val, string format)
{
// If we have a null value then return an empty string
if (null == val)
{ return string.Empty; }
// If we have no format string then just return ToString on the
val
if (string.IsNullOrEmpty(format))
{ return val.ToString(); }
// Split into the 3 possible format forms
// posative #s;negative #s;zeros
string[] formats = format.Split(';');
// For comparisons against 0 convert ValueType to a double as it
is the
// most precise of the numerical types so we won't inaccuratly
compare the
// value this way.
System.Reflection.MethodInfo compareMethod =
val.GetType().GetMethod("CompareTo", new Type[] { val.GetType() });
if (null == compareMethod)
{
// throw ex;
}
int rVal = (int)compareMethod.Invoke(val, new object[] { 0 });
int formatIdx = -1;
if (0 < rVal && 1 <= format.Length) // Number is posative use
format 1
{ formatIdx = 0; }
else if (0 > rVal && 2 <= format.Length) // Number is negative
use format 2
{ formatIdx = 1; }
else if (0 == rVal && 3 <= format.Length) // Number is equal to
0 use format 3
{ formatIdx = 2; }
// If we have a posative value
if (-1 < formatIdx)
{
// Simple regex expression. Any # of ,'s followed by a
period(.) and then a tilda(~)
string reg = ",+\\.~";
Regex regx = new Regex(reg);
Match match = regx.Match(formats[formatIdx]);
if (match.Success)
{
// Now figure out how many ,'s exist before the . and
adjust
// the 1000's divide value accordingly. (each comma
represents a thousnds power)
int idx = match.Value.IndexOf('.') + 1;
long a = (int)System.Math.Pow(1000, idx);
System.Reflection.MethodInfo divide =
val.GetType().GetMethod("operator /", new Type[] { typeof(int) });
val = val/a;
format = Regex.Replace(format, reg, string.Empty);
}
}
System.Reflection.MethodInfo tostringMethod =
val.GetType().GetMethod("ToString", new Type[] {typeof(string)});
if (null == tostringMethod)
{
// throw ex;
}
return (string)tostringMethod.Invoke(val, new object[] { format
});
}