2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FLclNumberFormatterEx.cpp
19 * @brief This is the implementation file for the NumberFormatterEx class.
21 #include <unique_ptr.h>
22 #include <unicode/decimfmt.h>
24 #include <FBaseSysLog.h>
25 #include <FLclNumberFormatter.h>
27 #include "FLcl_LocaleData.h"
28 #include "FLcl_LocaleImpl.h"
29 #include "FLcl_LocaleManagerImpl.h"
30 #include "FLcl_NumberFormatterImpl.h"
33 using namespace Tizen::Base;
35 namespace Tizen { namespace Locales
38 _NumberFormatterImpl::_NumberFormatterImpl(void)
39 : __pIcuNumberFormatter(null)
43 _NumberFormatterImpl::~_NumberFormatterImpl(void)
45 delete __pIcuNumberFormatter;
49 _NumberFormatterImpl::CreateNumberFormatterN(NumberFormatterStyle style)
51 return CreateNumberFormatterN(_LocaleManagerImpl::GetSystemLocale(), style);
55 _NumberFormatterImpl::CreateNumberFormatterN(const Locale& locale, NumberFormatterStyle style)
57 IcuLocale icuLocale = _LocaleImpl::GetLocaleImpl(locale)->GetIcuLocale();
58 SysTryReturn(NID_LCL, !icuLocale.isBogus(), null, E_UNSUPPORTED_OPERATION,
59 "[%s] Given locale is not supported", GetErrorMessage(E_UNSUPPORTED_OPERATION));
61 std::unique_ptr<NumberFormatter> pNewFormatter(new (std::nothrow) NumberFormatter);
62 SysTryReturn(NID_LCL, pNewFormatter, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
64 std::unique_ptr<_NumberFormatterImpl> pNumberFormatterImpl(new (std::nothrow) _NumberFormatterImpl);
65 SysTryReturn(NID_LCL, pNumberFormatterImpl, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed");
67 UNumberFormatStyle icuStyle = UNUM_FORMAT_STYLE_COUNT;
70 case NUM_FORMATTER_STYLE_NUMBER:
71 icuStyle = UNUM_DECIMAL;
73 case NUM_FORMATTER_STYLE_CURRENCY:
74 icuStyle = UNUM_CURRENCY;
76 case NUM_FORMATTER_STYLE_PERCENT:
77 icuStyle = UNUM_PERCENT;
79 case NUM_FORMATTER_STYLE_INVALID:
83 UErrorCode ec = U_ZERO_ERROR;
84 std::unique_ptr<IcuNumberFormat> pIcuNumberFormatter(IcuNumberFormat::createInstance(icuLocale, icuStyle, ec));
85 if (pIcuNumberFormatter)
87 IcuDecimalFormat* pDecimalFmt = dynamic_cast<IcuDecimalFormat*>(pIcuNumberFormatter.get());
90 pIcuNumberFormatter.release();
91 pNumberFormatterImpl->__pIcuNumberFormatter = pDecimalFmt;
92 pNumberFormatterImpl->__currency.Construct(locale);
93 pNewFormatter->__pNumberFormatterImpl = pNumberFormatterImpl.release();
95 SetLastResult(E_SUCCESS);
96 return pNewFormatter.release();
100 SetLastResult(E_INVALID_ARG);
105 _NumberFormatterImpl::ApplyPattern(const String& pattern, bool localized)
107 UErrorCode ec = U_ZERO_ERROR;
110 __pIcuNumberFormatter->applyLocalizedPattern(_LocaleData::GetIcuString(pattern), ec);
114 __pIcuNumberFormatter->applyPattern(_LocaleData::GetIcuString(pattern), ec);
117 return U_SUCCESS(ec) ? E_SUCCESS : E_INVALID_ARG;
121 _NumberFormatterImpl::Format(long number, String& str, _FieldPosition pos) const
123 IcuUnicodeString icuStr;
124 IcuFieldPosition icuPos = _LocaleData::GetIcuFieldPosition(pos);
125 icuStr = __pIcuNumberFormatter->format((int64_t)number, icuStr, icuPos);
126 str = _LocaleData::GetOspString(icuStr);
131 _NumberFormatterImpl::Format(double number, String& str, _FieldPosition pos) const
133 IcuUnicodeString icuStr;
134 IcuFieldPosition icuPos = _LocaleData::GetIcuFieldPosition(pos);
135 icuStr = __pIcuNumberFormatter->format(number, icuStr, icuPos);
136 str = _LocaleData::GetOspString(icuStr);
141 _NumberFormatterImpl::GetCurrency(void)
147 _NumberFormatterImpl::SetCurrency(const Currency& newValue)
149 __currency.Construct(newValue.GetCurrencyCode());
150 __pIcuNumberFormatter->setCurrency(_LocaleData::GetIcuString(newValue.GetCurrencyCode()).getBuffer());
154 _NumberFormatterImpl::GetMaxIntegerDigits(void) const
156 return __pIcuNumberFormatter->getMaximumIntegerDigits();
160 _NumberFormatterImpl::SetMaxIntegerDigits(int newValue)
162 __pIcuNumberFormatter->setMaximumIntegerDigits(newValue);
166 _NumberFormatterImpl::GetMinIntegerDigits(void) const
168 return __pIcuNumberFormatter->getMinimumIntegerDigits();
172 _NumberFormatterImpl::SetMinIntegerDigits(int newValue)
174 __pIcuNumberFormatter->setMinimumIntegerDigits(newValue);
178 _NumberFormatterImpl::GetMaxFractionDigits(void) const
180 return __pIcuNumberFormatter->getMaximumFractionDigits();
184 _NumberFormatterImpl::SetMaxFractionDigits(int newValue)
186 __pIcuNumberFormatter->setMaximumFractionDigits(newValue);
190 _NumberFormatterImpl::GetMinFractionDigits(void) const
192 return __pIcuNumberFormatter->getMinimumFractionDigits();
196 _NumberFormatterImpl::SetMinFractionDigits(int newValue)
198 __pIcuNumberFormatter->setMinimumFractionDigits(newValue);
202 _NumberFormatterImpl::IsGroupingUsed(void) const
204 return __pIcuNumberFormatter->isGroupingUsed();
208 _NumberFormatterImpl::SetGroupingUsed(bool newValue)
210 __pIcuNumberFormatter->setGroupingUsed(newValue);
214 _NumberFormatterImpl::GetPositivePrefix(void) const
216 IcuUnicodeString icuStr;
217 return _LocaleData::GetOspString(__pIcuNumberFormatter->getPositivePrefix(icuStr));
221 _NumberFormatterImpl::SetPositivePrefix(const String& newValue)
223 __pIcuNumberFormatter->setPositivePrefix(_LocaleData::GetIcuString(newValue));
227 _NumberFormatterImpl::GetNegativePrefix(void) const
229 IcuUnicodeString icuStr;
230 return _LocaleData::GetOspString(__pIcuNumberFormatter->getNegativePrefix(icuStr));
234 _NumberFormatterImpl::SetNegativePrefix(const String& newValue)
236 __pIcuNumberFormatter->setNegativePrefix(_LocaleData::GetIcuString(newValue));
240 _NumberFormatterImpl::GetPositiveSuffix(void) const
242 IcuUnicodeString icuStr;
243 return _LocaleData::GetOspString(__pIcuNumberFormatter->getPositiveSuffix(icuStr));
247 _NumberFormatterImpl::SetPositiveSuffix(const String& newValue)
249 __pIcuNumberFormatter->setPositiveSuffix(_LocaleData::GetIcuString(newValue));
253 _NumberFormatterImpl::GetNegativeSuffix(void) const
255 IcuUnicodeString icuStr;
256 return _LocaleData::GetOspString(__pIcuNumberFormatter->getNegativeSuffix(icuStr));
260 _NumberFormatterImpl::SetNegativeSuffix(const String& newValue)
262 __pIcuNumberFormatter->setNegativeSuffix(_LocaleData::GetIcuString(newValue));
266 _NumberFormatterImpl::GetMultiplier(void) const
268 return __pIcuNumberFormatter->getMultiplier();
272 _NumberFormatterImpl::SetMultiplier(int newValue)
274 __pIcuNumberFormatter->setMultiplier(newValue);
279 _NumberFormatterImpl::GetGroupingSize(void) const
281 return __pIcuNumberFormatter->getGroupingSize();
285 _NumberFormatterImpl::SetGroupingSize(int newValue)
287 __pIcuNumberFormatter->setGroupingSize(newValue);
292 _NumberFormatterImpl::IsDecimalSeparatorAlwaysShown(void) const
294 return __pIcuNumberFormatter->isDecimalSeparatorAlwaysShown();
298 _NumberFormatterImpl::SetDecimalSeparatorAlwaysShown(bool newValue)
300 __pIcuNumberFormatter->setDecimalSeparatorAlwaysShown(newValue);
304 _NumberFormatterImpl::IsPositiveSignAlwaysShown(void) const
306 IcuUnicodeString ps("+");
308 pp = __pIcuNumberFormatter->getPositivePrefix(pp);
314 _NumberFormatterImpl::SetPositiveSignAlwaysShown(bool newValue)
316 IcuUnicodeString ps("+");
318 pp = __pIcuNumberFormatter->getPositivePrefix(pp);
322 __pIcuNumberFormatter->setPositivePrefix(ps);
328 __pIcuNumberFormatter->setPositivePrefix("");
333 } } // Tizen::Locales