Is there a way to force the precision of a decimal variable?

  • Thread starter Thread starter Douglas Harber
  • Start date Start date
D

Douglas Harber

If I have a decimal value, say foo:

decimal foo = 3;

when I serialize that decimal value to XML, I'll get an element like this:

<foo>3</foo>

If I have a value like:

decimal foo=3.00M;

serializing to XML produces:

<foo>3.00</foo>

My problem is that if I have a textbox and a user types in a value of "3", I
need to parse it into a decimal variable and end up with 3.00. (Alternately,
if there was a way to tell XML serialization to output "3" as "3.00", that'd
be fine but there doesn't appear to be any way to do that.)

Is there a way to force the precision of a parsed decimal value?

Thanks,
Doug Harber
 
Douglas Harber said:
If I have a decimal value, say foo:

decimal foo = 3;

when I serialize that decimal value to XML, I'll get an element like this:

<foo>3</foo>

If I have a value like:

decimal foo=3.00M;

serializing to XML produces:

<foo>3.00</foo>

My problem is that if I have a textbox and a user types in a value of "3", I
need to parse it into a decimal variable and end up with 3.00. (Alternately,
if there was a way to tell XML serialization to output "3" as "3.00", that'd
be fine but there doesn't appear to be any way to do that.)

Is there a way to force the precision of a parsed decimal value?

There are a few ways. Firstly, you could manipulate it as a string and
use decimal.Parse. Secondly, you could manipulate the stored format -
convert the decimal to 4 ints, alter them appropriately, then convert
it back.

See http://www.pobox.com/~skeet/csharp/decimal.html for a bit more
information. The Decimal type provides ways of converting between
Decimal itself and the 4 ints.
 
why not use some numeric box instead of Text Box.
By the way you can use string numeric formatting.
I am developing a numeric box. And providing u the partial code. Hope this
helps.
Take a look at BuildExpressions() and Validate() functions.

public class NumericBox : System.Windows.Forms.TextBox
{
private char _CurrencySymbol;
private bool _AllowNegative;
private bool _AllowGroupSeperator;
private bool _AllowAccountingFormat;
private int _NumericPrecision;
private int _NumericScale;
private string _RegularExpression;
private string _FormatString;
private string _NumericText;
private string _DisplayText;
public NumericBox()
{
_CurrencySymbol='\0';
_AllowNegative=false;
_AllowAccountingFormat=false;
_AllowGroupSeperator=false;
_NumericPrecision=1;
_NumericScale=0;
_NumericText="";
_DisplayText="";
base.Text="";
BuildExpressions();
}
[Description("Currency symbol to be displayed with the numeric
value."),DefaultValue('\0')]
public char CurrencySymbol
{
set
{
_CurrencySymbol=value;
BuildExpressions();
Validate();
}
get
{
return _CurrencySymbol;
}
}
[Description("Allow negative numeric value."),DefaultValue(false)]
public bool AllowNegative
{
set
{
_AllowNegative=value;
BuildExpressions();
Validate();
}
get
{
return _AllowNegative;
}
}
[Description("Allow group seperator between digits."),DefaultValue(false)]
public bool AllowGroupSeperator
{
set
{
_AllowGroupSeperator=value;
BuildExpressions();
Validate();
}
get
{
return _AllowGroupSeperator;
}
}
[Description("Allow accounting format to display negative numeric
value."),DefaultValue(false)]
public bool AllowAccountingFormat
{
set
{
_AllowAccountingFormat=value;
BuildExpressions();
Validate();
}
get
{
return _AllowAccountingFormat;
}
}
[Description("Precision of numeric value."),DefaultValue(1)]
public int NumericPrecision
{
set
{
if(value>=0)
{
_NumericPrecision=value;
BuildExpressions();
Validate();
}
}
get
{
return _NumericPrecision;
}
}
[Description("Scale of numeric value."),DefaultValue(0)]
public int NumericScale
{
set
{
if(value>=0)
{
_NumericScale=value;
BuildExpressions();
Validate();
}
}
get
{
return _NumericScale;
}
}
[ReadOnly(true),DefaultValue("")]
public new string Text
{
get
{
return _DisplayText;
}
}
[Description("Numeric string value."),DefaultValue("")]
public string NumericText
{
set
{
_NumericText=value.Trim();
Validate();
}
get
{
return _NumericText;
}
}
private void BuildExpressions()
{
_RegularExpression = (_AllowNegative)?@"(\+|-)?":@"\+?";
_RegularExpression += (_NumericPrecision==0)?@"0":@"[0-9]{1,"+
_NumericPrecision.ToString() +"}";
_RegularExpression += (_NumericScale==0)?@"":@"(\.[0-9]{1,"+
_NumericScale.ToString() +"})?";

_FormatString=(_CurrencySymbol=='\0')?@"":_CurrencySymbol.ToString();
_FormatString+=(_AllowGroupSeperator)?@"#,##0":@"#0";
if(_NumericScale!=0)
{
_FormatString+=".";
for(int i=1;i<=_NumericScale;i++)
_FormatString+="0";
}
_FormatString+=(_AllowAccountingFormat)?@";("+ _FormatString +");":@"";
}
private void Validate()
{
Regex r = new Regex(_RegularExpression);
Match m = r.Match(_NumericText);
if(m!=null)
_NumericText=m.ToString();
else
_NumericText="";
if(_NumericText!="")
{
_DisplayText=Decimal.Parse(_NumericText).ToString(_FormatString);
_NumericText=Decimal.Parse(_NumericText).ToString();
}
else
_DisplayText="";
if(this.Focused)
{
base.Text=_NumericText;
}
else
{
base.Text=_DisplayText;
}
}
protected override void OnEnter(EventArgs e)
{
base.Text=_NumericText;
this.Select(base.Text.Length,0);
base.OnEnter(e);
}
protected override void OnLeave(EventArgs e)
{
base.Text=_DisplayText;
base.OnLeave(e);
}
}
 
Back
Top