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 <serval2412@yahoo.fr>
This commit is contained in:
parent
8ce5b63148
commit
98f88ac1ff
3 changed files with 25 additions and 3 deletions
|
@ -232,6 +232,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);
|
||||
|
|
|
@ -3298,8 +3298,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)
|
||||
|
@ -3382,6 +3381,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
|
||||
|
@ -3413,10 +3414,23 @@ void SbRtl_FormatNumber(StarBASIC*, SbxArray& rPar, bool)
|
|||
else
|
||||
aResult.insert(0, '-');
|
||||
}
|
||||
|
||||
if (isPercent)
|
||||
aResult.append('%');
|
||||
rPar.Get(0)->PutString(aResult.makeStringAndClear());
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
||||
// note: BASIC does not use comphelper::random, because
|
||||
|
|
|
@ -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),
|
||||
|
||||
|
|
Loading…
Reference in a new issue