diff options
author | Caolán McNamara <caolanm@redhat.com> | 2014-01-20 10:41:32 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2014-01-20 11:19:18 +0000 |
commit | c2866a9da4822fe886683efabe223db97d0296cb (patch) | |
tree | dcbdd5425431958f028fa2af7b70e858790b6d60 /i18nutil/source/utility | |
parent | daaa6425ba689e69227751d4f4a719c01977ff1e (diff) |
Related: #i56998# provide a way to format % per-locale rules
Change-Id: Ic27b230cc9dce366f281ff720ded5873e94f6191
Diffstat (limited to 'i18nutil/source/utility')
-rw-r--r-- | i18nutil/source/utility/unicode.cxx | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/i18nutil/source/utility/unicode.cxx b/i18nutil/source/utility/unicode.cxx index 009896987b6e..56daacdacd1a 100644 --- a/i18nutil/source/utility/unicode.cxx +++ b/i18nutil/source/utility/unicode.cxx @@ -17,10 +17,14 @@ * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ +#include <boost/scoped_ptr.hpp> #include <com/sun/star/i18n/UnicodeType.hpp> #include <com/sun/star/i18n/KCharacterType.hpp> #include <com/sun/star/i18n/ScriptType.hpp> +#include <i18nlangtag/languagetag.hxx> +#include <i18nlangtag/languagetagicu.hxx> #include <i18nutil/unicode.hxx> +#include <unicode/numfmt.h> #include "unicode_data.h" // Workaround for glibc braindamage: @@ -934,4 +938,26 @@ OString SAL_CALL unicode::getExemplarLanguageForUScriptCode(UScriptCode eScript) return sRet; } +//Format a number as a percentage according to the rules of the given +//language, e.g. 100 -> "100%" for en-US vs "100 %" for de-DE +OUString SAL_CALL unicode::formatPercent(double dNumber, + const LanguageTag &rLangTag) +{ + // get a currency formatter for this locale ID + UErrorCode errorCode=U_ZERO_ERROR; + icu::Locale aLocale = LanguageTagIcu::getIcuLocale(rLangTag); + boost::scoped_ptr<NumberFormat> xF( + NumberFormat::createPercentInstance(aLocale, errorCode)); + if(U_FAILURE(errorCode)) + { + SAL_WARN("i18n", "NumberFormat::createPercentInstance failed"); + return OUString::number(dNumber) + "%"; + } + + UnicodeString output; + xF->format(dNumber, output); + return OUString(reinterpret_cast<const sal_Unicode *>(output.getBuffer()), + output.length()); +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |