Skip to content

Commit

Permalink
Issue #48 implemented odd and even functions
Browse files Browse the repository at this point in the history
  • Loading branch information
swmal committed Feb 29, 2020
1 parent 989ffc2 commit 43c41ac
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/EPPlus/FormulaParsing/Excel/Functions/BuiltInFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public BuiltInFunctions()
Functions["large"] = new Large();
Functions["small"] = new Small();
Functions["degrees"] = new Degrees();
Functions["odd"] = new Odd();
Functions["even"] = new Even();
// Information
Functions["isblank"] = new IsBlank();
Functions["isnumber"] = new IsNumber();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime
{
public class WorkdayIntl : ExcelFunction
internal class WorkdayIntl : ExcelFunction
{
public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
Expand Down
49 changes: 49 additions & 0 deletions src/EPPlus/FormulaParsing/Excel/Functions/Math/Even.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/
A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************
Date Author Change
*************************************************************************************************
01/27/2020 EPPlus Software AB Initial release EPPlus 5
*************************************************************************************************/
using OfficeOpenXml.FormulaParsing.ExpressionGraph;
using OfficeOpenXml.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OfficeOpenXml.FormulaParsing.Excel.Functions.Math
{
internal class Even : ExcelFunction
{
public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
ValidateArguments(arguments, 1);
var arg = arguments.ElementAt(0).Value;
if (!IsNumeric(arg)) return new CompileResult(eErrorType.Value);
var number = ConvertUtil.GetValueDouble(arg);
if (number % 1 != 0)
{
if (number >= 0)
{
number = number - (number % 1) + 1;
}
else
{
number = number - (number % 1) - 1;
}
}
var intNumber = Convert.ToInt32(number);
if (intNumber % 2 == 1)
{
intNumber = intNumber >= 0 ? intNumber + 1 : intNumber - 1;
}
return CreateResult(intNumber, DataType.Integer);
}
}
}
49 changes: 49 additions & 0 deletions src/EPPlus/FormulaParsing/Excel/Functions/Math/Odd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*************************************************************************************************
Required Notice: Copyright (C) EPPlus Software AB.
This software is licensed under PolyForm Noncommercial License 1.0.0
and may only be used for noncommercial purposes
https://polyformproject.org/licenses/noncommercial/1.0.0/
A commercial license to use this software can be purchased at https://epplussoftware.com
*************************************************************************************************
Date Author Change
*************************************************************************************************
01/27/2020 EPPlus Software AB Initial release EPPlus 5
*************************************************************************************************/
using OfficeOpenXml.FormulaParsing.ExpressionGraph;
using OfficeOpenXml.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OfficeOpenXml.FormulaParsing.Excel.Functions.Math
{
internal class Odd : ExcelFunction
{
public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
ValidateArguments(arguments, 1);
var arg = arguments.ElementAt(0).Value;
if (!IsNumeric(arg)) return new CompileResult(eErrorType.Value);
var number = ConvertUtil.GetValueDouble(arg);
if(number % 1 > 0)
{
if (number >= 0)
{
number = number - (number % 1) + 1;
}
else
{
number = number - (number % 1) - 1;
}
}
var intNumber = Convert.ToInt32(number);
if(intNumber % 2 == 0)
{
intNumber = intNumber >= 0 ? intNumber + 1 : intNumber - 1;
}
return CreateResult(intNumber, DataType.Integer);
}
}
}
105 changes: 105 additions & 0 deletions src/EPPlusTest/FormulaParsing/Excel/Functions/MathFunctionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,111 @@ public void CountIfShouldHandleNegativeCriteria()
Assert.AreEqual(1d, sheet1.Cells["A3"].Value);
}
}

[TestMethod]
public void OddShouldRound0To1()
{
using (var package = new ExcelPackage())
{
var sheet1 = package.Workbook.Worksheets.Add("test");
sheet1.Cells["A1"].Value = 0;
sheet1.Cells["A3"].Formula = "ODD(A1)";
sheet1.Calculate();
Assert.AreEqual(1, sheet1.Cells["A3"].Value);
}
}

[TestMethod]
public void OddShouldRound1To1()
{
using (var package = new ExcelPackage())
{
var sheet1 = package.Workbook.Worksheets.Add("test");
sheet1.Cells["A1"].Value = 1;
sheet1.Cells["A3"].Formula = "ODD(A1)";
sheet1.Calculate();
Assert.AreEqual(1, sheet1.Cells["A3"].Value);
}
}

[TestMethod]
public void OddShouldRound2To3()
{
using (var package = new ExcelPackage())
{
var sheet1 = package.Workbook.Worksheets.Add("test");
sheet1.Cells["A1"].Value = 2;
sheet1.Cells["A3"].Formula = "ODD(A1)";
sheet1.Calculate();
Assert.AreEqual(3, sheet1.Cells["A3"].Value);
}
}

[TestMethod]
public void OddShouldRoundMinus1point3ToMinus3()
{
using (var package = new ExcelPackage())
{
var sheet1 = package.Workbook.Worksheets.Add("test");
sheet1.Cells["A1"].Value = -1.3;
sheet1.Cells["A3"].Formula = "ODD(A1)";
sheet1.Calculate();
Assert.AreEqual(-3, sheet1.Cells["A3"].Value);
}
}

[TestMethod]
public void EvenShouldRound0To0()
{
using (var package = new ExcelPackage())
{
var sheet1 = package.Workbook.Worksheets.Add("test");
sheet1.Cells["A1"].Value = 0;
sheet1.Cells["A3"].Formula = "EVEN(A1)";
sheet1.Calculate();
Assert.AreEqual(0, sheet1.Cells["A3"].Value);
}
}

[TestMethod]
public void EvenShouldRound1To2()
{
using (var package = new ExcelPackage())
{
var sheet1 = package.Workbook.Worksheets.Add("test");
sheet1.Cells["A1"].Value = 1;
sheet1.Cells["A3"].Formula = "EVEN(A1)";
sheet1.Calculate();
Assert.AreEqual(2, sheet1.Cells["A3"].Value);
}
}

[TestMethod]
public void EvenShouldRound2To2()
{
using (var package = new ExcelPackage())
{
var sheet1 = package.Workbook.Worksheets.Add("test");
sheet1.Cells["A1"].Value = 2;
sheet1.Cells["A3"].Formula = "EVEN(A1)";
sheet1.Calculate();
Assert.AreEqual(2, sheet1.Cells["A3"].Value);
}
}

[TestMethod]
public void EvenShouldRoundMinus1point3ToMinus2()
{
using (var package = new ExcelPackage())
{
var sheet1 = package.Workbook.Worksheets.Add("test");
sheet1.Cells["A1"].Value = -1.3;
sheet1.Cells["A3"].Formula = "EVEN(A1)";
sheet1.Calculate();
Assert.AreEqual(-2, sheet1.Cells["A3"].Value);
}
}

[TestMethod]
public void Rank()
{
Expand Down

0 comments on commit 43c41ac

Please sign in to comment.