internal static string toLocale(NumPattern p, NumDigits d, NumberFormatInfo df)
{
// string buffer
StringBuilder s = new StringBuilder();
if (d.negative) s.Append(df.NegativeSign);
// if we have more frac digits then maxFrac, then round off
d.round(p.maxFrac);
// if we have an optional integer part, and only
// fractional digits, then don't include leading zero
int start = 0;
if (p.optInt && d.zeroInt()) start = d.dec;
// if min required fraction digits are zero and we
// have nothing but zeros, then truncate to a whole number
if (p.minFrac == 0 && d.zeroFrac(p.maxFrac)) d.size = d.dec;
// leading zeros
for (int i=0; i<p.minInt-d.dec; ++i) s.Append('0');
// walk thru the digits and apply locale symbols
bool dec = false;
for (int i=start; i<d.size; ++i)
{
if (i < d.dec)
{
if ((d.dec - i) % p.group == 0 && i > 0)
s.Append(df.NumberGroupSeparator);
}
else
{
if (i == d.dec && p.maxFrac > 0)
{
s.Append(df.NumberDecimalSeparator);
dec = true;
}
if (i-d.dec >= p.maxFrac) break;
}
s.Append(d.digits[i]);
}
// trailing zeros
for (int i=0; i<p.minFrac-d.fracSize(); ++i)
{
if (!dec) { s.Append(df.NumberDecimalSeparator); dec = true; }
s.Append('0');
}
// handle #.# case
if (s.Length == 0) return "0";
return s.ToString();
}