Skip to content

Commit

Permalink
Changed cast type to long to cover large numbers accurately (#1117)
Browse files Browse the repository at this point in the history
  • Loading branch information
OssianEPPlus authored Oct 23, 2023
1 parent 66f0be0 commit fe1aa8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/EPPlus/FormulaParsing/Excel/Functions/Math/Rounddown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, P
}
else
{
result = (int)System.Math.Floor(number);
result = (long)System.Math.Floor(number);
result = result - (result % System.Math.Pow(10, (nDecimals*-1)));
}
return CreateResult(result * nFactor, DataType.Decimal);
}

private static double RoundDownDecimalNumber(double number, int nDecimals)
{
int integerRepresentation = (int)System.Math.Floor(number * System.Math.Pow(10d, nDecimals));
long integerRepresentation = (long)System.Math.Floor(number * System.Math.Pow(10d, nDecimals));
var result = integerRepresentation / System.Math.Pow(10d, nDecimals);
return result;
}
Expand Down
15 changes: 15 additions & 0 deletions src/EPPlusTest/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Date Author Change
using OfficeOpenXml.Drawing.Style.Coloring;
using OfficeOpenXml.Export.HtmlExport.Interfaces;
using OfficeOpenXml.Filter;
using OfficeOpenXml.FormulaParsing;
using OfficeOpenXml.Sparkline;
using OfficeOpenXml.Style;
using OfficeOpenXml.Table;
Expand Down Expand Up @@ -5446,5 +5447,19 @@ public void i1110()
var tbl = sheet.Cells["A1:B10"].LoadFromDataTable(table, false, OfficeOpenXml.Table.TableStyles.Dark1);
}
}

[TestMethod]
public void i1087LongNumber()
{
using (var package = new ExcelPackage())
{
var sheet = package.Workbook.Worksheets.Add("test");
sheet.Cells["A1"].Value = 2347440000d;
sheet.Cells["A2"].Formula = "ROUNDDOWN(A1,-5)";
sheet.Calculate(opt => opt.PrecisionAndRoundingStrategy = PrecisionAndRoundingStrategy.Excel);

Assert.AreEqual(2347400000d, sheet.Cells["A2"].Value);
}
}
}
}

0 comments on commit fe1aa8c

Please sign in to comment.