Replicating Java's DecimalFormat in C#
- by Frank Krueger
I am trying to replicate a subset of Java's DecimalFormat class. Below is what I've come up with. Does this look right to everyone?
public class DecimalFormat : NumberFormat
{
int _maximumFractionDigits;
int _minimumFractionDigits;
string _format;
void RebuildFormat ()
{
_format = "{0:0.";
_format += new string ('0', _minimumFractionDigits);
if (_maximumFractionDigits > _minimumFractionDigits) {
_format += new string ('#', _maximumFractionDigits -
_minimumFractionDigits);
}
_format += "}";
}
public override string format (object value)
{
return string.Format (_format, value);
}
public override void setMaximumFractionDigits (int n)
{
_maximumFractionDigits = n;
RebuildFormat ();
}
public override void setMinimumFractionDigits (int n)
{
_minimumFractionDigits = n;
RebuildFormat ();
}
public override void setGroupingUsed (bool g)
{
}
public static NumberFormat getInstance ()
{
return new DecimalFormat ();
}
}