Пример #1
0
    static void Format(NumberStore ns, int precision)
    {
        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--;
                }
            }
        }
    }
Пример #2
0
		internal static string FormatCustom(string format, NumberStore ns, NumberFormatInfo nfi)
		{
			bool p = ns.Positive;
			int offset = 0;
			int length = 0;
			CustomInfo.GetActiveSection(format, ref p, ns.ZeroOnly, ref offset, ref length);
			if (length == 0)
			{
				return ns.Positive ? String.Empty : nfi.NegativeSign;
			}
			ns.Positive = p;

			CustomInfo info = CustomInfo.Parse(format, offset, length, nfi);
#if false
			Console.WriteLine("Format : {0}",format);
			Console.WriteLine("DecimalDigits : {0}",info.DecimalDigits);
			Console.WriteLine("DecimalPointPos : {0}",info.DecimalPointPos);
			Console.WriteLine("DecimalTailSharpDigits : {0}",info.DecimalTailSharpDigits);
			Console.WriteLine("IntegerDigits : {0}",info.IntegerDigits);
			Console.WriteLine("IntegerHeadSharpDigits : {0}",info.IntegerHeadSharpDigits);
			Console.WriteLine("IntegerHeadPos : {0}",info.IntegerHeadPos);
			Console.WriteLine("UseExponent : {0}",info.UseExponent);
			Console.WriteLine("ExponentDigits : {0}",info.ExponentDigits);
			Console.WriteLine("ExponentTailSharpDigits : {0}",info.ExponentTailSharpDigits);
			Console.WriteLine("ExponentNegativeSignOnly : {0}",info.ExponentNegativeSignOnly);
			Console.WriteLine("DividePlaces : {0}",info.DividePlaces);
			Console.WriteLine("Percents : {0}",info.Percents);
			Console.WriteLine("Permilles : {0}",info.Permilles);
#endif
			StringBuilder sb_int = new StringBuilder(info.IntegerDigits * 2);
			StringBuilder sb_dec = new StringBuilder(info.DecimalDigits * 2);
			StringBuilder sb_exp = (info.UseExponent ? new StringBuilder(info.ExponentDigits * 2) : null);

			int diff = 0;
			if (info.Percents > 0)
			{
				ns.Multiply10(2 * info.Percents);
			}
			if (info.Permilles > 0)
			{
				ns.Multiply10(3 * info.Permilles);
			}
			if (info.DividePlaces > 0)
			{
				ns.Divide10(info.DividePlaces);
			}

			bool expPositive = true;
			if (info.UseExponent && (info.DecimalDigits > 0 || info.IntegerDigits > 0))
			{
				if (!ns.ZeroOnly)
				{
					while (true)
					{
						while (ns.IntegerDigits > info.IntegerDigits)
						{
							ns.Divide10(1);
							diff--;
							if (ns.IntegerDigits == 1 && ns.GetChar(0) == '0')
								break;
						}
						while (ns.IntegerDigits < info.IntegerDigits || (ns.IntegerDigits == info.IntegerDigits && ns.GetChar(0) == '0'))
						{
							ns.Multiply10(1);
							diff++;
						}

						if (!ns.RoundDecimal(info.DecimalDigits))
							break;
					}
				}

				expPositive = diff <= 0;
				NumberStore.AppendIntegerStringFromUInt32(sb_exp, (uint)(diff >= 0 ? diff : -diff));
			}
			else
			{
				ns.RoundDecimal(info.DecimalDigits);
				if (ns.ZeroOnly)
					ns.Positive = true;
			}

			if (info.IntegerDigits != 0 || !ns.CheckZeroOnlyInteger())
			{
				ns.AppendIntegerString(ns.IntegerDigits, sb_int);
			}
			/* if (sb_int.Length > info.IntegerDigits) {
				int len = 0;
				while (sb_int.Length > info.IntegerDigits && len < sb_int.Length) {
					if (sb_int [len] == '0')
						len ++;
					else
						break;
				}
				sb_int.Remove (0, len);
			} */

			ns.AppendDecimalString(ns.DecimalDigits, sb_dec);

			if (info.UseExponent)
			{
				if (info.DecimalDigits <= 0 && info.IntegerDigits <= 0)
					ns.Positive = true;

				if (sb_int.Length < info.IntegerDigits)
					sb_int.Insert(0, "0", info.IntegerDigits - sb_int.Length);

				while (sb_exp.Length < info.ExponentDigits - info.ExponentTailSharpDigits)
					sb_exp.Insert(0, '0');

				if (expPositive && !info.ExponentNegativeSignOnly)
					sb_exp.Insert(0, nfi.PositiveSign);
				else if (!expPositive)
					sb_exp.Insert(0, nfi.NegativeSign);
			}
			else
			{
				if (sb_int.Length < info.IntegerDigits - info.IntegerHeadSharpDigits)
					sb_int.Insert(0, "0", info.IntegerDigits - info.IntegerHeadSharpDigits - sb_int.Length);
				if (info.IntegerDigits == info.IntegerHeadSharpDigits && NumberStore.IsZeroOnly(sb_int))
					sb_int.Remove(0, sb_int.Length);
			}

			ZeroTrimEnd(sb_dec, true);
			while (sb_dec.Length < info.DecimalDigits - info.DecimalTailSharpDigits)
				sb_dec.Append('0');
			if (sb_dec.Length > info.DecimalDigits)
				sb_dec.Remove(info.DecimalDigits, sb_dec.Length - info.DecimalDigits);

			return info.Format(format, offset, length, nfi, ns.Positive, sb_int, sb_dec, sb_exp);
		}
Пример #3
0
		internal static string FormatPercent(NumberStore ns, int precision, NumberFormatInfo nfi)
		{
			precision = (precision >= 0 ? precision : nfi.PercentDecimalDigits);
			ns.Multiply10(2);
			ns.RoundDecimal(precision);
			bool needNegativeSign = (!ns.Positive && !ns.ZeroOnly);

			StringBuilder sb = new StringBuilder(ns.IntegerDigits * 2 + precision + 16);

			if (!needNegativeSign)
			{
				if (nfi.PercentPositivePattern == 2)
				{
					sb.Append(nfi.PercentSymbol);
				}
			}
			else
			{
				switch (nfi.PercentNegativePattern)
				{
					case 0:
						sb.Append(nfi.NegativeSign);
						break;
					case 1:
						sb.Append(nfi.NegativeSign);
						break;
					case 2:
						sb.Append(nfi.NegativeSign);
						sb.Append(nfi.PercentSymbol);
						break;
				}
			}

			ns.AppendIntegerStringWithGroupSeparator(sb, nfi.PercentGroupSizes, nfi.PercentGroupSeparator);

			if (precision > 0)
			{
				sb.Append(nfi.PercentDecimalSeparator);
				ns.AppendDecimalString(precision, sb);
			}

			if (!needNegativeSign)
			{
				switch (nfi.PercentPositivePattern)
				{
					case 0:
						sb.Append(' ');
						sb.Append(nfi.PercentSymbol);
						break;
					case 1:
						sb.Append(nfi.PercentSymbol);
						break;
				}
			}
			else
			{
				switch (nfi.PercentNegativePattern)
				{
					case 0:
						sb.Append(' ');
						sb.Append(nfi.PercentSymbol);
						break;
					case 1:
						sb.Append(nfi.PercentSymbol);
						break;
				}
			}

			return sb.ToString();
		}
Пример #4
0
		internal static string FormatExponential(NumberStore ns, int precision, NumberFormatInfo nfi, bool upper)
		{
			if (precision < 0)
				precision = 6;

			if (ns.ZeroOnly)
			{
				StringBuilder sb = new StringBuilder(precision + nfi.PositiveSign.Length + 6);
				sb.Append('0');
				if (precision > 0)
				{
					sb.Append('.');
					sb.Append('0', precision);
				}

				if (upper)
					sb.Append('E');
				else
					sb.Append('e');

				sb.Append(nfi.PositiveSign);
				sb.Append('0', 3);

				return sb.ToString();
			}

			int exponent = 0;
			while (!(ns.DecimalPointPosition == 1 && ns.GetChar(0) != '0'))
			{
				if (ns.DecimalPointPosition > 1)
				{
					ns.Divide10(1);
					exponent++;
				}
				else
				{
					ns.Multiply10(1);
					exponent--;
				}
			}

			if (ns.RoundDecimal(precision))
			{
				ns.Divide10(1);
				exponent++;
			}

			StringBuilder cb = new StringBuilder(ns.DecimalDigits + 1 + 8);

			if (!ns.Positive)
			{
				cb.Append(nfi.NegativeSign);
			}

			ns.AppendIntegerString(ns.IntegerDigits > 0 ? ns.IntegerDigits : 1, cb);

			if (precision > 0)
			{
				cb.Append(nfi.NumberDecimalSeparator);
				ns.AppendDecimalString(precision, cb);
			}

			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', 3);
			}
			else if (exponent < 10)
			{
				cb.Append('0', 2);
				cb.Append(digitLowerTable[exponent]);
			}
			else if (exponent < 100)
			{
				cb.Append('0', 1);
				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]);
				/*} else { // exponent range is 0...+-324
					int pos = cb.Length;
					int count = 3;
					while (exponent > 0 || --count > 0) {
						cb.Insert (pos, digitLowerTable [exponent % 10]);
						exponent /= 10;
					}*/
			}

			return cb.ToString();
		}
Пример #5
0
		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();
		}