Skip to content

Commit

Permalink
Add MROUND exception when number and multiple parameters have differe…
Browse files Browse the repository at this point in the history
…nt signs
  • Loading branch information
igitur committed Nov 21, 2020
1 parent 55c44a7 commit 5d2f462
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 6 additions & 3 deletions ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -763,10 +763,13 @@ private static object Mod(List<Expression> p)

private static object MRound(List<Expression> p)
{
var n = (Double)p[0];
var k = (Double)p[1];
var number = (Double)p[0];
var multiple = (Double)p[1];

return Math.Round(n / k, MidpointRounding.AwayFromZero) * k;
if (Math.Sign(number) != Math.Sign(multiple))
throw new NumberException($"The Number and Multiple arguments must have the same sign.");

return Math.Round(number / multiple, MidpointRounding.AwayFromZero) * multiple;
}

private static object Multinomial(List<Expression> p)
Expand Down
7 changes: 7 additions & 0 deletions ClosedXML_Tests/Excel/CalcEngine/MathTrigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,13 @@ public double MRound(double number, double multiple)
return (double)XLWorkbook.EvaluateExpr($"MROUND({number}, {multiple})");
}

[TestCase(123456.123, -10)]
[TestCase(-123456.123, 5)]
public void MRoundExceptions(double number, double multiple)
{
Assert.Throws<NumberException>(() => XLWorkbook.EvaluateExpr($"MROUND({number}, {multiple})"));
}

[TestCase(0, 1)]
[TestCase(0.3, 1.0467516)]
[TestCase(0.6, 1.21162831)]
Expand Down

0 comments on commit 5d2f462

Please sign in to comment.