basic/source/inc/rtlproto.hxx | 1 + basic/source/runtime/methods.cxx | 19 +++++++++++++++++-- basic/source/runtime/stdobj.cxx | 7 +++++++ 3 files changed, 25 insertions(+), 2 deletions(-)
New commits: commit 975bc84af3b3b3d251e68bcb86b0d0e619e0987c Author: Julien Nabet <serval2...@yahoo.fr> AuthorDate: Mon Apr 18 19:37:10 2022 +0200 Commit: Xisco Fauli <xiscofa...@libreoffice.org> CommitDate: Fri Apr 22 18:46:41 2022 +0200 tdf#148651: implement VBA.FormatPercent I started from a copy/paste of FormatNumber. Then I deduplicated the code (it saved about 99% of it). Change-Id: Ibcb9ffbf8cebf45d5ffac4713e3d220b8499ba11 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/133133 Tested-by: Jenkins Reviewed-by: Julien Nabet <serval2...@yahoo.fr> Reviewed-on: https://gerrit.libreoffice.org/c/core/+/133246 Reviewed-by: Xisco Fauli <xiscofa...@libreoffice.org> diff --git a/basic/source/inc/rtlproto.hxx b/basic/source/inc/rtlproto.hxx index 7347ead45dc1..ff089d6b0dbd 100644 --- a/basic/source/inc/rtlproto.hxx +++ b/basic/source/inc/rtlproto.hxx @@ -233,6 +233,7 @@ extern void SbRtl_IsUnoStruct(StarBASIC * pBasic, SbxArray & rPar, bool bWrite); extern void SbRtl_FileDateTime(StarBASIC * pBasic, SbxArray & rPar, bool bWrite); extern void SbRtl_Format(StarBASIC * pBasic, SbxArray & rPar, bool bWrite); extern void SbRtl_FormatNumber(StarBASIC* pBasic, SbxArray& rPar, bool bWrite); +extern void SbRtl_FormatPercent(StarBASIC* pBasic, SbxArray& rPar, bool bWrite); extern void SbRtl_GetAttr(StarBASIC * pBasic, SbxArray & rPar, bool bWrite); extern void SbRtl_Randomize(StarBASIC * pBasic, SbxArray & rPar, bool bWrite); // JSM extern void SbRtl_Round(StarBASIC * pBasic, SbxArray & rPar, bool bWrite); diff --git a/basic/source/runtime/methods.cxx b/basic/source/runtime/methods.cxx index e678b8852838..851584b6db79 100644 --- a/basic/source/runtime/methods.cxx +++ b/basic/source/runtime/methods.cxx @@ -3337,8 +3337,7 @@ void SbRtl_Format(StarBASIC *, SbxArray & rPar, bool) } } -// https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/formatnumber-function -void SbRtl_FormatNumber(StarBASIC*, SbxArray& rPar, bool) +static void lcl_FormatNumberPercent(SbxArray& rPar, bool isPercent) { const sal_uInt32 nArgCount = rPar.Count(); if (nArgCount < 2 || nArgCount > 6) @@ -3421,6 +3420,8 @@ void SbRtl_FormatNumber(StarBASIC*, SbxArray& rPar, bool) } double fVal = rPar.Get(1)->GetDouble(); + if (isPercent) + fVal *= 100; const bool bNegative = fVal < 0; if (bNegative) fVal = fabs(fVal); // Always work with non-negatives, to easily handle leading zero @@ -3462,6 +3463,20 @@ void SbRtl_FormatNumber(StarBASIC*, SbxArray& rPar, bool) } rPar.Get(0)->PutString(aResult); + if (isPercent) + aResult += "%"; +} + +// https://docs.microsoft.com/en-us/office/vba/Language/Reference/User-Interface-Help/formatnumber-function +void SbRtl_FormatNumber(StarBASIC*, SbxArray& rPar, bool) +{ + return lcl_FormatNumberPercent(rPar, false); +} + +// https://docs.microsoft.com/en-us/office/vba/Language/Reference/User-Interface-Help/formatpercent-function +void SbRtl_FormatPercent(StarBASIC*, SbxArray& rPar, bool) +{ + return lcl_FormatNumberPercent(rPar, true); } namespace { diff --git a/basic/source/runtime/stdobj.cxx b/basic/source/runtime/stdobj.cxx index dcbc0ec05983..810f53ad4912 100644 --- a/basic/source/runtime/stdobj.cxx +++ b/basic/source/runtime/stdobj.cxx @@ -401,6 +401,13 @@ constexpr Method aMethods[] = { arg(u"useParensForNegativeNumbers", SbxINTEGER, OPT_), // vbTriState arg(u"groupDigits", SbxINTEGER, OPT_), // vbTriState +{ u"FormatPercent", SbxSTRING, 5 | FUNCTION_ | COMPATONLY_, SbRtl_FormatPercent }, + arg(u"expression", SbxDOUBLE), + arg(u"numDigitsAfterDecimal", SbxINTEGER, OPT_), + arg(u"includeLeadingDigit", SbxINTEGER, OPT_), // vbTriState + arg(u"useParensForNegativeNumbers", SbxINTEGER, OPT_), // vbTriState + arg(u"groupDigits", SbxINTEGER, OPT_), // vbTriState + { u"Frac", SbxDOUBLE, 1 | FUNCTION_, SbRtl_Frac }, arg(u"number", SbxDOUBLE),