Ejemplo n.º 1
0
            // writers

            public void WriteItem(object value)
            {
                if (value == null)
                {
                    WriteMarker(Marker.Null);
                }
                else
                {
                    var type = value.GetType();

                    if (Int29Types.Contains(type))
                    {
                        WriteInt29(Convert.ToInt32(value));
                    }
                    else if (NumberTypes.Contains(type))
                    {
                        WriteDouble(Convert.ToDouble(value));
                    }
                    else if (Writers.TryGetValue(type, out var writer))
                    {
                        writer(this, value);
                    }
                    else
                    {
                        DispatchGenericWrite(value);
                    }
                }
            }
        public void Does_not_use_custom_decimal()
        {
#if NETCORE
            CsvConfig.RealNumberCultureInfo = new CultureInfo("nl-NL");
#else
            CsvConfig.RealNumberCultureInfo = CultureInfo.CreateSpecificCulture("nl-NL");
#endif

            var num = new NumberTypes
            {
                Int     = 1111,
                Float   = 2222.2222f,
                Double  = 3333.3333,
                Decimal = 4444.4444M,
            };

            num.ToJson().Print();
            num.ToJsv().Print();
            num.ToCsv().Print();

            Assert.That(num.ToJson(), Does.Contain("4444.4444"));
            Assert.That(num.ToJsv(), Does.Contain("4444.4444"));
            Assert.That(num.ToCsv(), Does.Contain("4444,4444"));

            CsvConfig.RealNumberCultureInfo = null;
        }
Ejemplo n.º 3
0
        internal static ReturnConverter MakeReturnConversion(Type returnType)
        {
            if (returnType == typeof(void))
            {
                return((e, s, o) => MondValue.Undefined);
            }

            if (returnType == typeof(MondValue))
            {
                return((e, s, o) => ReferenceEquals(o, null) ? MondValue.Null : (MondValue)o);
            }

            if (returnType == typeof(string))
            {
                return((e, s, o) => ReferenceEquals(o, null) ? MondValue.Null : (MondValue)(string)o);
            }

            if (returnType == typeof(bool))
            {
                return((e, s, o) => (bool)o);
            }

            if (NumberTypes.Contains(returnType))
            {
                return((e, s, o) => Convert.ToDouble(o));
            }

            var classAttrib = returnType.Attribute <MondClassAttribute>();

            if (classAttrib != null && classAttrib.AllowReturn)
            {
                var className = classAttrib.Name ?? returnType.Name;

                return((e, s, o) =>
                {
                    var prototype = s.FindPrototype(className);
                    if (prototype == null)
                    {
                        throw new MondRuntimeException(e + string.Format(BindingError.PrototypeNotFound, className));
                    }

                    var obj = new MondValue(s);
                    obj.Prototype = prototype;
                    obj.UserData = o;
                    return obj;
                });
            }

            throw new MondBindingException(BindingError.UnsupportedReturnType, returnType);
        }
Ejemplo n.º 4
0
        // MondValue -> T
        internal static Func <Expression, Expression> MakeParameterConversion(Type parameterType)
        {
            if (BasicTypes.Contains(parameterType))
            {
                return(e => Expression.Convert(e, parameterType));
            }

            if (NumberTypes.Contains(parameterType))
            {
                return(e => Expression.Convert(Expression.Convert(e, typeof(double)), parameterType));
            }

            return(null);
        }
Ejemplo n.º 5
0
            void WriteItemInternal(object value)
            {
                var type = value.GetType();

                if (NumberTypes.Contains(type))
                {
                    WriteNumber(Convert.ToDouble(value));
                }
                else if (Writers.TryGetValue(type, out var write))
                {
                    write(this, value);
                }
                else
                {
                    DispatchGenericWrite(value);
                }
            }
Ejemplo n.º 6
0
 public TableCell(double val)
 {
     ValueType   = CellValues.Number;
     _numberType = NumberTypes.Double;
     _doubleVal  = val;
 }
Ejemplo n.º 7
0
 public TableCell(int val)
 {
     ValueType   = CellValues.Number;
     _numberType = NumberTypes.Integer;
     _iVal       = val;
 }
Ejemplo n.º 8
0
 public static NumericLiteralSyntax NumericLiteral(string pValue, NumberTypes pType)
 {
     return(new NumericLiteralSyntax(pValue, pType));
 }
Ejemplo n.º 9
0
        // T -> MondValue
        internal static ReturnConverter MakeReturnConversion(Type returnType)
        {
            if (returnType == typeof(void))
            {
                return((e, s, v) => throw new MondBindingException("Expression binder should not use void return conversion"));
            }

            if (returnType == typeof(MondValue))
            {
                return((e, s, v) => Expression.Coalesce(v, Expression.Constant(MondValue.Null)));
            }

            if (returnType == typeof(string))
            {
                return((e, s, v) =>
                {
                    var tempVar = Expression.Variable(typeof(string));

                    var tempVarIsNull = Expression.ReferenceEqual(tempVar, Expression.Constant(null));
                    var tempVarAsValue = Expression.Convert(tempVar, typeof(MondValue));

                    var block = Expression.Block(
                        new[] { tempVar },
                        Expression.Assign(tempVar, v),
                        Expression.Condition(tempVarIsNull, Expression.Constant(MondValue.Null), tempVarAsValue)
                        );

                    return block;
                });
            }

            if (BasicTypes.Contains(returnType))
            {
                return((e, s, v) => Expression.Convert(v, typeof(MondValue)));
            }

            if (NumberTypes.Contains(returnType))
            {
                return((e, s, v) => Expression.Convert(Expression.Convert(v, typeof(double)), typeof(MondValue)));
            }

            var classAttrib = returnType.Attribute <MondClassAttribute>();

            if (classAttrib != null && classAttrib.AllowReturn)
            {
                var objInitializerType = typeof(IEnumerable <KeyValuePair <MondValue, MondValue> >);
                var valueFactory       = typeof(MondValue).GetMethod("Object", new[] { typeof(MondState), objInitializerType });
                if (valueFactory == null)
                {
                    throw new Exception("Could not find MondValue.Object method");
                }

                var findPrototype = typeof(MondState).GetMethod("FindPrototype");
                if (findPrototype == null)
                {
                    throw new Exception("Could not find FindPrototype method");
                }

                var className = classAttrib.Name ?? returnType.Name;

                return((e, s, v) =>
                {
                    var prototype = Expression.Variable(typeof(MondValue));
                    var obj = Expression.Variable(typeof(MondValue));

                    var throwMsg = Expression.Constant(e + string.Format(BindingError.PrototypeNotFound, className));
                    var throwExpr = Expression.Throw(Expression.New(RuntimeExceptionConstructor, throwMsg));

                    return Expression.Block(
                        new [] { prototype, obj },
                        Expression.Assign(prototype, Expression.Call(s, findPrototype, Expression.Constant(className))),
                        Expression.IfThen(Expression.ReferenceEqual(prototype, Expression.Constant(null)), throwExpr),
                        Expression.Assign(obj, Expression.Call(valueFactory, s, Expression.Constant(null, objInitializerType))),
                        Expression.Assign(Expression.PropertyOrField(obj, "Prototype"), prototype),
                        Expression.Assign(Expression.PropertyOrField(obj, "UserData"), v),
                        obj
                        );
                });
            }

            throw new MondBindingException(BindingError.UnsupportedReturnType, returnType);
        }
Ejemplo n.º 10
0
        protected ExcelRange FormatNumber(ExcelRange cell, NumberTypes Type = NumberTypes.NUMBER, Precision Decimals = Precision.ZERO)
        {
            switch (Type)
            {
            case NumberTypes.CURRENCY:
                switch (Decimals)
                {
                case Precision.ZERO:
                    cell.Style.Numberformat.Format = "_($* #,##0_);_($* (#,##0);_($* \"-\"??_);_(@_)";
                    break;

                case Precision.ONE:
                    cell.Style.Numberformat.Format = "_($* #,##0.0_);_($* (#,##0.0);_($* \"-\"??_);_(@_)";
                    break;

                case Precision.TWO:
                    cell.Style.Numberformat.Format = "_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(@_)";
                    break;

                case Precision.THREE:
                    cell.Style.Numberformat.Format = "_($* #,##0.000_);_($* (#,##0.000);_($* \"-\"??_);_(@_)";
                    break;

                case Precision.FOUR:
                    cell.Style.Numberformat.Format = "_($* #,##0.0000_);_($* (#,##0.0000);_($* \"-\"??_);_(@_)";
                    break;

                case Precision.FIVE:
                    cell.Style.Numberformat.Format = "_($* #,##0.00000_);_($* (#,##0.00000);_($* \"-\"??_);_(@_)";
                    break;

                case Precision.SIX:
                    cell.Style.Numberformat.Format = "_($* #,##0.000000_);_($* (#,##0.000000);_($* \"-\"??_);_(@_)";
                    break;

                case Precision.SEVEN:
                    cell.Style.Numberformat.Format = "_($* #,##0.0000000_);_($* (#,##0.0000000);_($* \"-\"??_);_(@_)";
                    break;

                default:
                    cell.Style.Numberformat.Format = "_($* #,##0.00000000_);_($* (#,##0.00000000);_($* \"-\"??_);_(@_)";
                    break;
                }
                break;

            case NumberTypes.PERCENTAGE:
                switch (Decimals)
                {
                case Precision.ZERO:
                    cell.Style.Numberformat.Format = "#,##0 %";
                    break;

                case Precision.ONE:
                    cell.Style.Numberformat.Format = "#,##0.0 %";
                    break;

                case Precision.TWO:
                    cell.Style.Numberformat.Format = "#,##0.00 %";
                    break;

                case Precision.THREE:
                    cell.Style.Numberformat.Format = "#,##0.000 %";
                    break;

                case Precision.FOUR:
                    cell.Style.Numberformat.Format = "#,##0.0000 %";
                    break;

                case Precision.FIVE:
                    cell.Style.Numberformat.Format = "#,##0.00000 %";
                    break;

                case Precision.SIX:
                    cell.Style.Numberformat.Format = "#,##0.000000 %";
                    break;

                case Precision.SEVEN:
                    cell.Style.Numberformat.Format = "#,##0.0000000 %";
                    break;

                default:
                    cell.Style.Numberformat.Format = "#,##0 %";
                    break;
                }
                break;

            case NumberTypes.NUMBER:
            default:
                switch (Decimals)
                {
                case Precision.ZERO:
                    cell.Style.Numberformat.Format = "###,###,##0";
                    break;

                case Precision.ONE:
                    cell.Style.Numberformat.Format = "###,###,##0.0";
                    break;

                case Precision.TWO:
                    cell.Style.Numberformat.Format = "###,###,##0.00";
                    break;

                case Precision.THREE:
                    cell.Style.Numberformat.Format = "###,###,##0.000";
                    break;

                case Precision.FOUR:
                    cell.Style.Numberformat.Format = "###,###,##0.0000";
                    break;

                case Precision.FIVE:
                    cell.Style.Numberformat.Format = "###,###,##0.00000";
                    break;

                case Precision.SIX:
                    cell.Style.Numberformat.Format = "###,###,##0.000000";
                    break;

                case Precision.SEVEN:
                    cell.Style.Numberformat.Format = "###,###,##0.0000000";
                    break;

                default:
                    cell.Style.Numberformat.Format = "###,###,##0";
                    break;
                }
                break;
            }
            return(cell);
        }
Ejemplo n.º 11
0
 protected ExcelRange InsertFormula(string value, int colOffset = 0, Precision Decimals = Precision.ZERO, NumberTypes Type = NumberTypes.NUMBER, int?col = null, int?row = null)
 {
     return(InsertValue(value, "formula", colOffset, Decimals: Decimals, Type: Type, col: col, row: row));
 }
Ejemplo n.º 12
0
        private ExcelRange InsertValue(object value, string sType, int offset, TextAlign AlignMode = TextAlign.LEFT, Precision Decimals = Precision.ZERO, NumberTypes Type = NumberTypes.NUMBER, int mergeCols = 1, int?rowsOccuppied = null, float fontSize = 9, bool fontBold = false, ExcelVerticalAlignment VerticalAlign = ExcelVerticalAlignment.Center, int?col = null, int?row = null)
        {
            int targetColumn;
            int targetRow;

            if (col != null)
            {
                targetColumn = (int)col;
            }
            else
            {
                colIndex    += offset;
                targetColumn = colIndex;
            }

            if (row != null)
            {
                targetRow = (int)row;
            }
            else
            {
                targetRow = rowIndex;
            }


            if (targetColumn > maxColIndex)
            {
                maxColIndex = targetColumn;
            }

            if (mergeCols > 1)
            {
                if (targetColumn + mergeCols > maxColIndex)
                {
                    maxColIndex = targetColumn + mergeCols;
                }
            }

            if (rowsOccuppied != null)
            {
                ws.Row(rowIndex).Height = (int)rowsOccuppied * baseRowHeight;
            }

            ExcelRange cell = ws.Cells[targetRow, targetColumn];

            if (inTable)
            {
                TD(cell);
            }

            switch (sType)
            {
            case "formula":
                FormatNumber(cell, Type: Type, Decimals: Decimals);
                if (value == null)
                {
                    cell.Value = "";
                }
                else
                {
                    cell.Formula = (string)value;
                }
                break;

            case "string":
                switch (AlignMode)
                {
                case TextAlign.LEFT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                    break;

                case TextAlign.CENTER:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    break;

                case TextAlign.RIGHT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    break;

                default:
                    break;
                }
                cell.Style.VerticalAlignment = VerticalAlign;
                cell.Style.Font.Size         = fontSize;
                cell.Style.Font.Bold         = fontBold;
                if (string.IsNullOrWhiteSpace((string)value))
                {
                    cell.Value = null;
                }
                else
                {
                    cell.Value = (string)value;
                }
                break;

            case "paragraph":
                if (mergeCols > 1)
                {
                    cell       = ws.Cells[cell.Address + ":" + cell.Offset(0, mergeCols).Address];
                    cell.Merge = true;
                }
                cell.Style.WrapText = true;

                switch (AlignMode)
                {
                case TextAlign.LEFT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                    break;

                case TextAlign.CENTER:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    break;

                case TextAlign.RIGHT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    break;

                default:
                    break;
                }
                if (value == null)
                {
                    cell.Value = "";
                }
                else
                {
                    cell.Value = (string)value;
                }
                break;

            case "number":
            case "double":
                FormatNumber(cell, Type: NumberTypes.NUMBER, Decimals: Decimals);
                if (value == null)
                {
                    cell.Value = 0;
                }
                else
                {
                    cell.Value = value;
                    try
                    {
                        if ((decimal)(double)value < 0)
                        {
                            cell.Style.Font.Color.SetColor(Color.Red);
                        }
                    }
                    catch { }
                }
                break;

            case "int":
                FormatNumber(cell, Type: NumberTypes.NUMBER, Decimals: 0);
                if (value == null)
                {
                    cell.Value = 0;
                }
                else
                {
                    cell.Value = value;
                    try
                    {
                        if ((int)value < 0)
                        {
                            cell.Style.Font.Color.SetColor(Color.Red);
                        }
                    }
                    catch { }
                }
                break;

            case "currency":
                FormatNumber(cell, Type: NumberTypes.CURRENCY, Decimals: Decimals);
                if (value == null)
                {
                    cell.Value = 0;
                }
                else
                {
                    cell.Value = value;
                    try
                    {
                        if ((decimal)(double)value < 0)
                        {
                            cell.Style.Font.Color.SetColor(Color.Red);
                        }
                    }
                    catch { }
                }
                break;

            case "percentage":
                FormatNumber(cell, Type: NumberTypes.PERCENTAGE, Decimals: Decimals);

                switch (AlignMode)
                {
                case TextAlign.LEFT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                    break;

                case TextAlign.CENTER:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    break;

                case TextAlign.RIGHT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    break;

                default:
                    break;
                }

                if (value == null)
                {
                    cell.Value = 0;
                }
                else
                {
                    cell.Value = (double)value;
                    try
                    {
                        if ((double)value < 0)
                        {
                            cell.Style.Font.Color.SetColor(Color.Red);
                        }
                    }
                    catch { }
                }
                break;

            case "title":
                cell.Style.Font.Size    = 18;
                ws.Row(rowIndex).Height = 23;

                switch (AlignMode)
                {
                case TextAlign.LEFT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                    break;

                case TextAlign.CENTER:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    break;

                case TextAlign.RIGHT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    break;

                default:
                    break;
                }
                cell.Style.VerticalAlignment = ExcelVerticalAlignment.Bottom;
                cell.Value = (string)value;
                break;

            case "subtitle":
                cell.Style.Font.Bold = true;
                cell.Style.Font.Size = 14;
                switch (AlignMode)
                {
                case TextAlign.LEFT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                    break;

                case TextAlign.CENTER:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    break;

                case TextAlign.RIGHT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    break;

                default:
                    break;
                }
                cell.Value = (string)value;
                break;

            case "label":
                cell.Style.Font.Bold = true;
                cell.Style.WrapText  = true;
                switch (AlignMode)
                {
                case TextAlign.LEFT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                    break;

                case TextAlign.CENTER:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    break;

                case TextAlign.RIGHT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    break;

                default:
                    break;
                }
                cell.Value = (string)value;
                break;

            case "date":
                cell.Style.Numberformat.Format = "dd-mmm-yyyy";

                switch (AlignMode)
                {
                case TextAlign.LEFT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                    break;

                case TextAlign.CENTER:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    break;

                case TextAlign.RIGHT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    break;

                default:
                    break;
                }
                if (value == null)
                {
                    cell.Value = "";
                }
                else
                {
                    DateTime theDate = (DateTime)value;
                    cell.Formula = "=DATE(" + theDate.Year + "," + theDate.Month + "," + theDate.Day + ")";
                }
                break;

            case "datetime":
                cell.Style.Numberformat.Format = "dd-mmm-yyyy h:mm";

                switch (AlignMode)
                {
                case TextAlign.LEFT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left;
                    break;

                case TextAlign.CENTER:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    break;

                case TextAlign.RIGHT:
                    cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                    break;

                default:
                    break;
                }
                if (value == null)
                {
                    cell.Value = "";
                }
                else
                {
                    DateTime theDate = (DateTime)value;
                    cell.Formula = "=DATE(" + theDate.Year + "," + theDate.Month + "," + theDate.Day + ") + TIME(" + theDate.Hour + "," + theDate.Minute + "," + theDate.Second + ")";
                }
                break;

            case "bool":
                cell.Value = value;
                break;
            }
            if (col == null && row == null)
            {
                tab();
            }

            return(cell);
        }
Ejemplo n.º 13
0
 protected bool Equals(NumberTypes other)
 {
     return Int == other.Int && Float.Equals(other.Float) && Double.Equals(other.Double) && Decimal == other.Decimal;
 }
Ejemplo n.º 14
0
 protected bool Equals(NumberTypes other)
 {
     return(Int == other.Int && Float.Equals(other.Float) && Double.Equals(other.Double) && Decimal == other.Decimal);
 }
Ejemplo n.º 15
0
 void GetEnum()
 {
     number = SimplePrefs.GetData <NumberTypes>("numberType", true);
 }
Ejemplo n.º 16
0
 internal NumericLiteralSyntax(string pValue, NumberTypes pType) : base(pValue)
 {
     NumberType = pType;
 }