From e0503c6d489d4b3c7ae5b2f36b6935b60c319f63 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Wed, 29 Jul 2020 16:38:52 +0000 Subject: [PATCH] fpspreadsheet: Add new formula function ROUNDDOWN(). git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7586 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../fpspreadsheet/source/common/fpsfunc.pas | 31 +++++++++++++++++++ .../fpspreadsheet/source/common/xlsconst.pas | 1 + 2 files changed, 32 insertions(+) diff --git a/components/fpspreadsheet/source/common/fpsfunc.pas b/components/fpspreadsheet/source/common/fpsfunc.pas index f72afd617..286f7fe2e 100644 --- a/components/fpspreadsheet/source/common/fpsfunc.pas +++ b/components/fpspreadsheet/source/common/fpsfunc.pas @@ -436,6 +436,36 @@ begin end; end; +function MyRoundDown(const AValue: Double; const Digits: TRoundToRange): Double; +var + RV: Double; +begin + RV := IntPower(10, Digits); + Result := Trunc(AValue / RV) * RV; +end; + + +{ The Excel ROUNDDOWN function returns a number rounded down to a given number + of decimal places. Unlike standard rounding, where only numbers less than 5 + are rounded down, ROUNDDOWN rounds all numbers down. } +procedure fpsROUNDDOWN(var Result: TsExpressionResult; const Args: TsExprParameterArray); +var + x: TsExprFloat; + n: Integer; +begin + x := ArgToFloat(Args[1]); + if IsNaN(x) then + Result := ErrorResult(errWrongType) + else begin + n := Round(x); + x := ArgToFloat(Args[0]); + if IsNaN(x) then + Result := ErrorResult(errWrongType) + else + Result := FloatResult(MyRoundDown(x, -n)); + end; +end; + procedure fpsSIGN(var Result: TsExpressionResult; const Args: TsExprParameterArray); var x: TsExprFloat; @@ -2296,6 +2326,7 @@ begin AddFunction(cat, 'RADIANS', 'F', 'F', INT_EXCEL_SHEET_FUNC_RADIANS, @fpsRADIANS); AddFunction(cat, 'RAND', 'F', '', INT_EXCEL_SHEET_FUNC_RAND, @fpsRAND); AddFunction(cat, 'ROUND', 'F', 'FF', INT_EXCEL_SHEET_FUNC_ROUND, @fpsROUND); + AddFunction(cat, 'ROUNDDOWN', 'F', 'F', INT_EXCEL_SHEET_FUNC_ROUNDDOWN, @fpsROUNDDOWN); AddFunction(cat, 'SIGN', 'F', 'F', INT_EXCEL_SHEET_FUNC_SIGN, @fpsSIGN); AddFunction(cat, 'SIN', 'F', 'F', INT_EXCEL_SHEET_FUNC_SIN, @fpsSIN); AddFunction(cat, 'SINH', 'F', 'F', INT_EXCEL_SHEET_FUNC_SINH, @fpsSINH); diff --git a/components/fpspreadsheet/source/common/xlsconst.pas b/components/fpspreadsheet/source/common/xlsconst.pas index b40443d12..8b27b31ee 100644 --- a/components/fpspreadsheet/source/common/xlsconst.pas +++ b/components/fpspreadsheet/source/common/xlsconst.pas @@ -234,6 +234,7 @@ const // No BIFF2 after 199 + INT_EXCEL_SHEET_FUNC_ROUNDDOWN = 213; // not available in BIFF2 INT_EXCEL_SHEET_FUNC_TODAY = 221; // not available in BIFF2 INT_EXCEL_SHEET_FUNC_MEDIAN = 227; // not available in BIFF2 INT_EXCEL_SHEET_FUNC_SINH = 229; // not available in BIFF2