private static string FormatGeneral(NumberStore ns, int precision, NumberFormatInfo nfi, bool upper, bool roundtrip) { if (ns.ZeroOnly) return "0"; precision = precision > 0 ? precision : ns.DefaultPrecision; int exponent = 0; bool expMode = (ns.IsDecimalSource && precision == ns.DefaultPrecision ? false : (ns.IntegerDigits > precision || ns.DecimalPointPosition <= -4)); if (expMode) { while (!(ns.DecimalPointPosition == 1 && ns.GetChar(0) != '0')) { if (ns.DecimalPointPosition > 1) { ns.Divide10(1); exponent++; } else { ns.Multiply10(1); exponent--; } } } precision = precision < ns.DefaultPrecision + 2 ? (precision < ns.DefaultMaxPrecision ? precision : ns.DefaultMaxPrecision) : ns.DefaultPrecision + 2; StringBuilder cb = new StringBuilder(ns.IntegerDigits + precision + 16); if (expMode) { if (ns.RoundDecimal(precision - 1)) { ns.Divide10(1); exponent++; } } else if (!roundtrip) { if (ns.IsDecimalSource) ns.RoundPos(precision); else ns.RoundDecimal(precision, true, false); } if (!ns.Positive) { cb.Append(nfi.NegativeSign); } ns.AppendIntegerString(ns.IntegerDigits > 0 ? ns.IntegerDigits : 1, cb); if (ns.DecimalDigits > 0) { cb.Append(nfi.NumberDecimalSeparator); ns.AppendDecimalString(ns.DecimalDigits, cb); } if (expMode) { if (upper) cb.Append('E'); else cb.Append('e'); if (exponent >= 0) cb.Append(nfi.PositiveSign); else { cb.Append(nfi.NegativeSign); exponent = -exponent; } if (exponent == 0) { cb.Append('0', 2); } else if (exponent < 10) { cb.Append('0'); cb.Append(digitLowerTable[exponent]); } else if (exponent < 100) { cb.Append(digitLowerTable[exponent / 10 % 10]); cb.Append(digitLowerTable[exponent % 10]); } else if (exponent < 1000) { cb.Append(digitLowerTable[exponent / 100 % 10]); cb.Append(digitLowerTable[exponent / 10 % 10]); cb.Append(digitLowerTable[exponent % 10]); } } return cb.ToString(); }