diff options
author | Laurent Balland-Poirier <laurent.balland-poirier@laposte.net> | 2015-05-03 15:09:20 +0200 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2015-05-06 10:45:50 +0000 |
commit | f630f9598f2e328cbe37aff5af0e93c027a94de5 (patch) | |
tree | 7b089aa8b8b4ab412037a298c4b0fc2e2308dad4 /svl/source | |
parent | a5a17610d5eff80387c7330527793de87cc82e58 (diff) |
tdf#90258 Toggle Thousand Separator with Engineering Notation
If scientific format is selected, "Thousands separator" option is almost
useless. It could be replaced by "Engineering Notation".
Rebase of https://gerrit.libreoffice.org/15152
Update with more robust tests.
Change-Id: Ie2b88b1f149fce26c32a43ace623cf1f45f38e6e
Reviewed-on: https://gerrit.libreoffice.org/15606
Reviewed-by: Eike Rathke <erack@redhat.com>
Tested-by: Eike Rathke <erack@redhat.com>
Diffstat (limited to 'svl/source')
-rw-r--r-- | svl/source/numbers/zforlist.cxx | 77 |
1 files changed, 64 insertions, 13 deletions
diff --git a/svl/source/numbers/zforlist.cxx b/svl/source/numbers/zforlist.cxx index df271c31dd42..04ee5d51a097 100644 --- a/svl/source/numbers/zforlist.cxx +++ b/svl/source/numbers/zforlist.cxx @@ -1797,6 +1797,15 @@ sal_uInt16 SvNumberFormatter::GetFormatPrecision( sal_uInt32 nFormat ) const return pFormatScanner->GetStandardPrec(); } +sal_uInt16 SvNumberFormatter::GetFormatIntegerDigits( sal_uInt32 nFormat ) const +{ + const SvNumberformat* pFormat = GetFormatEntry( nFormat ); + if ( pFormat ) + return pFormat->GetFormatIntegerDigits(); + else + return 1; +} + sal_Unicode SvNumberFormatter::GetDecSep() const { return GetNumDecimalSep()[0]; @@ -2590,12 +2599,45 @@ void SvNumberFormatter::ImpGetNegCurrFormat(OUStringBuffer& sNegStr, const OUStr rCurrSymbol, xLocaleData->getCurrNegativeFormat() ); } +sal_Int32 SvNumberFormatter::ImpPosToken ( const OUStringBuffer & sFormat, sal_Unicode token, sal_Int32 nStartPos /* = 0*/ ) +{ + sal_Int32 nLength = sFormat.getLength(); + for ( sal_Int32 i=nStartPos; i<nLength && i>=0 ; i++ ) + { + switch(sFormat[i]) + { + case '\"' : // skip text + i = sFormat.indexOf('\"',i+1); + break; + case '[' : // skip condition + i = sFormat.indexOf(']',i+1); + break; + case '\\' : // skip escaped character + i++; + break; + case ';' : + if (token == ';') + return i; + break; + case 'e' : + case 'E' : + if (token == 'E') + return i; // if 'E' is outside "" and [] it must be the 'E' exponent + break; + default : break; + } + if ( i<0 ) + i--; + } + return -2; +} + OUString SvNumberFormatter::GenerateFormat(sal_uInt32 nIndex, LanguageType eLnge, bool bThousand, bool IsRed, sal_uInt16 nPrecision, - sal_uInt16 nAnzLeading) + sal_uInt16 nLeadingZeros) { if (eLnge == LANGUAGE_DONTKNOW) { @@ -2606,7 +2648,8 @@ OUString SvNumberFormatter::GenerateFormat(sal_uInt32 nIndex, ImpGenerateCL(eLnge); // create new standard formats if necessary utl::DigitGroupingIterator aGrouping( xLocaleData->getDigitGrouping()); - const sal_Int32 nDigitsInFirstGroup = aGrouping.get(); + // always group of 3 for Engineering notation + const sal_Int32 nDigitsInFirstGroup = ( bThousand && (eType == css::util::NumberFormat::SCIENTIFIC) ) ? 3 : aGrouping.get(); const OUString& rThSep = GetNumThousandSep(); SvNumberformat* pFormat = GetFormatEntry( nIndex ); @@ -2614,20 +2657,27 @@ OUString SvNumberFormatter::GenerateFormat(sal_uInt32 nIndex, OUStringBuffer sString; using comphelper::string::padToLength; - if (nAnzLeading == 0) + if (nLeadingZeros == 0) { if (!bThousand) sString.append('#'); else { - sString.append('#'); - sString.append(rThSep); - padToLength(sString, sString.getLength() + nDigitsInFirstGroup, '#'); + if (eType == css::util::NumberFormat::SCIENTIFIC) + { // for scientific, bThousand is used for Engineering notation + sString.append("###"); + } + else + { + sString.append('#'); + sString.append(rThSep); + padToLength(sString, sString.getLength() + nDigitsInFirstGroup, '#'); + } } } else { - for (i = 0; i < nAnzLeading; i++) + for (i = 0; i < nLeadingZeros; i++) { if (bThousand && i > 0 && i == aGrouping.getPos()) { @@ -2636,11 +2686,12 @@ OUString SvNumberFormatter::GenerateFormat(sal_uInt32 nIndex, } sString.insert(0, '0'); } - if (bThousand && nAnzLeading < nDigitsInFirstGroup + 1) + if ( bThousand ) { - for (i = nAnzLeading; i < nDigitsInFirstGroup + 1; i++) + sal_Int32 nDigits = (eType == css::util::NumberFormat::SCIENTIFIC) ? 3*((nLeadingZeros-1)/3 + 1) : nDigitsInFirstGroup + 1; + for (i = nLeadingZeros; i < nDigits; i++) { - if (bThousand && i % nDigitsInFirstGroup == 0) + if ( i % nDigitsInFirstGroup == 0 ) sString.insert(0, rThSep); sString.insert(0, '#'); } @@ -2658,11 +2709,11 @@ OUString SvNumberFormatter::GenerateFormat(sal_uInt32 nIndex, else if (eType == css::util::NumberFormat::SCIENTIFIC) { OUStringBuffer sOldFormatString = pFormat->GetFormatstring(); - sal_Int32 nIndexE = sOldFormatString.indexOf('E'); + sal_Int32 nIndexE = ImpPosToken( sOldFormatString, 'E' ); if (nIndexE > -1) { - sal_Int32 nIndexSep = sOldFormatString.indexOf(';'); - if (nIndexSep > -1) + sal_Int32 nIndexSep = ImpPosToken( sOldFormatString, ';', nIndexE ); + if (nIndexSep > nIndexE) sString.append( sOldFormatString.copy(nIndexE, nIndexSep - nIndexE) ); else sString.append( sOldFormatString.copy(nIndexE) ); |