Skip to content

Commit

Permalink
Adding implicit conversion to decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros committed Mar 15, 2012
1 parent 32fb7e5 commit 632a5b1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Evaluant.Calculator.Tests/Fixtures.cs
Expand Up @@ -615,6 +615,15 @@ public void ShouldShortCircuitBooleanExpressions()

Assert.AreEqual(false, e.Evaluate());
}

[TestMethod]
public void ShouldAddDoubleAndDecimal()
{
var e = new Expression("1.8 + Abs([var1])");
e.Parameters["var1"] = 9.2;

Assert.AreEqual(11M, e.Evaluate());
}
}
}

8 changes: 4 additions & 4 deletions Evaluant.Calculator/Numbers.cs
Expand Up @@ -198,7 +198,7 @@ public static object Add(object a, object b)
case TypeCode.UInt64: return (Single)a + (UInt64)b;
case TypeCode.Single: return (Single)a + (Single)b;
case TypeCode.Double: return (Single)a + (Double)b;
case TypeCode.Decimal: throw new InvalidOperationException("Operator '+' can't be applied to operands of types 'float' and 'decimal'");
case TypeCode.Decimal: return Convert.ToDecimal(a) + (Decimal)b;
}
break;

Expand All @@ -216,7 +216,7 @@ public static object Add(object a, object b)
case TypeCode.UInt64: return (Double)a + (UInt64)b;
case TypeCode.Single: return (Double)a + (Single)b;
case TypeCode.Double: return (Double)a + (Double)b;
case TypeCode.Decimal: throw new InvalidOperationException("Operator '+' can't be applied to operands of types 'double' and 'decimal'");
case TypeCode.Decimal: return Convert.ToDecimal(a) + (Decimal)b;
}
break;

Expand All @@ -232,8 +232,8 @@ public static object Add(object a, object b)
case TypeCode.UInt32: return (Decimal)a + (UInt32)b;
case TypeCode.Int64: return (Decimal)a + (Int64)b;
case TypeCode.UInt64: return (Decimal)a + (UInt64)b;
case TypeCode.Single: throw new InvalidOperationException("Operator '+' can't be applied to operands of types 'decimal' and 'float'");
case TypeCode.Double: throw new InvalidOperationException("Operator '+' can't be applied to operands of types 'decimal' and 'double'");
case TypeCode.Single: return (Decimal)a + Convert.ToDecimal(b);
case TypeCode.Double: return (Decimal)a + Convert.ToDecimal(b);
case TypeCode.Decimal: return (Decimal)a + (Decimal)b;
}
break;
Expand Down

0 comments on commit 632a5b1

Please sign in to comment.