Beispiel #1
0
        public void PercentPrecision()
        {
            var numberFormatter = new NumberFormatter(precision: 0, percent: true);

            Assert.AreEqual("-1500 %", numberFormatter.Format(-15));
            Assert.AreEqual("27 %", numberFormatter.Format(0.267));
        }
Beispiel #2
0
        public void Percent()
        {
            var numberFormatter = new NumberFormatter(percent: true);

            Assert.AreEqual("-1500.00 %", numberFormatter.Format(-15));
            Assert.AreEqual("26.70 %", numberFormatter.Format(0.267));
        }
Beispiel #3
0
        public void DecimalPrecision()
        {
            var numberFormatter = new NumberFormatter(precision: 1, _decimal: true);

            Assert.AreEqual("-15.0", numberFormatter.Format(-15));
            Assert.AreEqual("0.3", numberFormatter.Format(0.267));
            Assert.AreEqual("0.0", numberFormatter.Format(1.23E-50));
        }
Beispiel #4
0
        public void ScientificPrecision()
        {
            var numberFormatter = new NumberFormatter(precision: 4, scientific: true);

            Assert.AreEqual("-1.5000E+001", numberFormatter.Format(-15));
            Assert.AreEqual("2.6700E-001", numberFormatter.Format(0.267));
            Assert.AreEqual("1.2300E+040", numberFormatter.Format(1.23E+40));
            Assert.AreEqual("1.2300E-050", numberFormatter.Format(1.23E-50));
        }
Beispiel #5
0
        public void Decimal()
        {
            var numberFormatter = new NumberFormatter(_decimal: true);

            Assert.IsFalse(numberFormatter.Format(-15).Contains("E"));
            Assert.IsFalse(numberFormatter.Format(0.267).Contains("E"));
            Assert.IsFalse(numberFormatter.Format(1.23E+40).Contains("E"));
            Assert.IsFalse(numberFormatter.Format(1.23E-50).Contains("E"));
        }
Beispiel #6
0
        public void DefaultFormat()
        {
            var numberFormatter = new NumberFormatter();

            Assert.AreEqual("-15", numberFormatter.Format(-15));
            Assert.AreEqual("0.267", numberFormatter.Format(0.267));
            Assert.AreEqual("1.23E+40", numberFormatter.Format(1.23E+40));
            Assert.AreEqual("1.23E-50", numberFormatter.Format(1.23E-50));
        }
Beispiel #7
0
        public void Scientific()
        {
            var numberFormatter = new NumberFormatter(scientific: true);

            Assert.IsTrue(numberFormatter.Format(-15).Contains("E"));
            Assert.IsTrue(numberFormatter.Format(0.267).Contains("E"));
            Assert.IsTrue(numberFormatter.Format(1.23E+40).Contains("E"));
            Assert.IsTrue(numberFormatter.Format(1.23E-50).Contains("E"));
        }
Beispiel #8
0
            public string Format(string format, object arg, IFormatProvider formatProvider)
            {
                if (arg == null)
                {
                    return(string.Empty);
                }
                if (arg is byte)
                {
                    return(NumberFormatter.Format(format, (int)(byte)arg, formatProvider));
                }
                if (arg is short)
                {
                    return(NumberFormatter.Format(format, (int)(short)arg, formatProvider));
                }
                if (arg is int)
                {
                    return(NumberFormatter.Format(format, (int)arg, formatProvider));
                }
                if (arg is long)
                {
                    return(NumberFormatter.Format(format, (long)arg, formatProvider));
                }
                if (arg is float)
                {
                    return(NumberFormatter.Format(format, (float)arg, formatProvider));
                }
                if (arg is double)
                {
                    return(NumberFormatter.Format(format, (double)arg, formatProvider));
                }
                if (arg is Type)
                {
                    return(((Type)arg).FullName);
                }
                if (arg is string)
                {
                    return((string)arg);
                }

                // default handling.
                var formattable = CompilerHelper.AsNativeIFormattable(arg);

                if (formattable != null)
                {
                    return(formattable.ToString(format, formatProvider));
                }
                return(arg.ToString());
            }
Beispiel #9
0
 public string Format(string format, object arg, IFormatProvider formatProvider)
 {
     if (arg is int)
     {
         return(NumberFormatter.Format(format, (int)arg, formatProvider));
     }
     if (arg is long)
     {
         return(NumberFormatter.Format(format, (long)arg, formatProvider));
     }
     if (arg is float)
     {
         return(NumberFormatter.Format(format, (float)arg, formatProvider));
     }
     if (arg is double)
     {
         return(NumberFormatter.Format(format, (double)arg, formatProvider));
     }
     throw new NotImplementedException("System.Globalization.NumberFormatInfo.CustomFormatter.Format");
 }
Beispiel #10
0
 public string Format(string format, object arg, IFormatProvider formatProvider)
 {
     if (arg is int)
     {
         return(NumberFormatter.Format(format, (int)arg, formatProvider));
     }
     if (arg is long)
     {
         return(NumberFormatter.Format(format, (long)arg, formatProvider));
     }
     if (arg is float)
     {
         return(NumberFormatter.Format(format, (float)arg, formatProvider));
     }
     if (arg is double)
     {
         return(NumberFormatter.Format(format, (double)arg, formatProvider));
     }
     if (arg is string)
     {
         return(null);               //should be handled by caller.
     }
     throw new NotImplementedException("System.Globalization.CultureInfo.CustomFormatter.Format");
 }
Beispiel #11
0
 public void FormatNullInput()
 {
     formatter.Format(null);
 }
Beispiel #12
0
        public static void WritePrimitiveValue(object value, object data, string literalValue, int maxStringLength, ref int column, NumberFormatter numberFormatter, Action <string, object, object> writer, Action <Role, string, object> writeToken)
        {
            if (literalValue != null)
            {
                Debug.Assert(data != null);
                writer(literalValue, null, data ?? BoxedTextColor.Text);
                column += literalValue.Length;
                return;
            }

            if (value == null)
            {
                // usually NullReferenceExpression should be used for this, but we'll handle it anyways
                writer("null", null, BoxedTextColor.Keyword);
                column += 4;
                return;
            }

            if (value is bool)
            {
                if ((bool)value)
                {
                    writer("true", null, BoxedTextColor.Keyword);
                    column += 4;
                }
                else
                {
                    writer("false", null, BoxedTextColor.Keyword);
                    column += 5;
                }
                return;
            }

            var s = value as string;

            if (s != null)
            {
                string tmp = "\"" + ConvertStringMaxLength(s, maxStringLength) + "\"";
                column += tmp.Length;
                writer(tmp, null, BoxedTextColor.String);
            }
            else if (value is char)
            {
                string tmp = "'" + ConvertCharLiteral((char)value) + "'";
                column += tmp.Length;
                writer(tmp, null, BoxedTextColor.Char);
            }
            else if (value is decimal)
            {
                string str = ((decimal)value).ToString(NumberFormatInfo.InvariantInfo) + "m";
                column += str.Length;
                writer(str, null, BoxedTextColor.Number);
            }
            else if (value is float)
            {
                float f = (float)value;
                if (float.IsInfinity(f) || float.IsNaN(f))
                {
                    // Strictly speaking, these aren't PrimitiveExpressions;
                    // but we still support writing these to make life easier for code generators.
                    writer("float", null, BoxedTextColor.Keyword);
                    column += 5;
                    writeToken(Roles.Dot, ".", BoxedTextColor.Operator);
                    if (float.IsPositiveInfinity(f))
                    {
                        writer("PositiveInfinity", null, BoxedTextColor.LiteralField);
                        column += "PositiveInfinity".Length;
                    }
                    else if (float.IsNegativeInfinity(f))
                    {
                        writer("NegativeInfinity", null, BoxedTextColor.LiteralField);
                        column += "NegativeInfinity".Length;
                    }
                    else
                    {
                        writer("NaN", null, BoxedTextColor.LiteralField);
                        column += 3;
                    }
                    return;
                }
                var number = f.ToString("R", NumberFormatInfo.InvariantInfo) + "f";
                if (f == 0 && 1 / f == float.NegativeInfinity)
                {
                    // negative zero is a special case
                    // (again, not a primitive expression, but it's better to handle
                    // the special case here than to do it in all code generators)
                    number = "-" + number;
                }
                column += number.Length;
                writer(number, value, BoxedTextColor.Number);
            }
            else if (value is double)
            {
                double f = (double)value;
                if (double.IsInfinity(f) || double.IsNaN(f))
                {
                    // Strictly speaking, these aren't PrimitiveExpressions;
                    // but we still support writing these to make life easier for code generators.
                    writer("double", null, BoxedTextColor.Keyword);
                    column += 6;
                    writeToken(Roles.Dot, ".", BoxedTextColor.Operator);
                    if (double.IsPositiveInfinity(f))
                    {
                        writer("PositiveInfinity", null, BoxedTextColor.LiteralField);
                        column += "PositiveInfinity".Length;
                    }
                    else if (double.IsNegativeInfinity(f))
                    {
                        writer("NegativeInfinity", null, BoxedTextColor.LiteralField);
                        column += "NegativeInfinity".Length;
                    }
                    else
                    {
                        writer("NaN", null, BoxedTextColor.LiteralField);
                        column += 3;
                    }
                    return;
                }
                string number = f.ToString("R", NumberFormatInfo.InvariantInfo);
                if (f == 0 && 1 / f == double.NegativeInfinity)
                {
                    // negative zero is a special case
                    // (again, not a primitive expression, but it's better to handle
                    // the special case here than to do it in all code generators)
                    number = "-" + number;
                }
                if (number.IndexOf('.') < 0 && number.IndexOf('E') < 0)
                {
                    number += ".0";
                }
                column += number.Length;
                writer(number, value, BoxedTextColor.Number);
            }
            else if (value is IFormattable)
            {
                string valueStr;
                switch (value)
                {
                case int v:
                    valueStr = numberFormatter.Format(v);
                    break;

                case uint v:
                    valueStr = numberFormatter.Format(v) + "U";
                    break;

                case long v:
                    valueStr = numberFormatter.Format(v) + "L";
                    break;

                case ulong v:
                    valueStr = numberFormatter.Format(v) + "UL";
                    break;

                case byte v:
                    valueStr = numberFormatter.Format(v);
                    break;

                case ushort v:
                    valueStr = numberFormatter.Format(v);
                    break;

                case short v:
                    valueStr = numberFormatter.Format(v);
                    break;

                case sbyte v:
                    valueStr = numberFormatter.Format(v);
                    break;

                default:
                    valueStr = ((IFormattable)value).ToString(null, NumberFormatInfo.InvariantInfo);
                    break;
                }
                writer(valueStr, value, BoxedTextColor.Number);
                column += valueStr.Length;
            }
            else
            {
                s = value.ToString();
                writer(s, null, CSharpMetadataTextColorProvider.Instance.GetColor(value));
                column += s.Length;
            }
        }
Beispiel #13
0
 /// <summary>
 /// Converts the numeric value of the current <see cref="Number"/> object to its equivalent string representation by using the specified format and culture-specific format information.
 /// </summary>
 /// <param name="format">A standard or custom numeric format string.</param>
 /// <param name="formatProvider"> An object that supplies culture-specific formatting information.</param>
 /// <returns>The string representation of the current <see cref="Number"/> value in the format specified by the <paramref name="formatProvider"/> and <paramref name="format"/> parameters.</returns>
 /// <exception cref="FormatException"></exception>
 /// <exception cref="InvalidOperationException">The precision is greater than <see cref="int.MaxValue" />.</exception>
 public readonly string ToString(string format, IFormatProvider formatProvider) => NumberFormatter.Format(this, format, formatProvider);
Beispiel #14
0
 private void FormatInput()
 {
     FormattedExpression = ExpressionFormatter.Format(RawExpression) + " =";
     FormattedResult     = NumberFormatter.Format(RawResult.ToString());
 }