Index: base/i18n/number_formatting.cc |
diff --git a/base/i18n/number_formatting.cc b/base/i18n/number_formatting.cc |
index 6f454a0848bdb3aa7fe0c1d3f5aafbb04be58030..6e592d5adddecd5132c2927536c852949584c5b0 100644 |
--- a/base/i18n/number_formatting.cc |
+++ b/base/i18n/number_formatting.cc |
@@ -23,28 +23,39 @@ namespace { |
// A simple wrapper around icu::NumberFormat that allows for resetting it |
// (as LazyInstance does not). |
-struct NumberFormatWrapper { |
- NumberFormatWrapper() { |
- Reset(); |
- } |
- |
- void Reset() { |
+struct FormatWrapper { |
+ void Reset(UNumberFormatStyle kind) { |
// There's no ICU call to destroy a NumberFormat object other than |
// operator delete, so use the default Delete, which calls operator delete. |
// This can cause problems if a different allocator is used by this file |
// than by ICU. |
UErrorCode status = U_ZERO_ERROR; |
- number_format.reset(icu::NumberFormat::createInstance(status)); |
+ number_format.reset(icu::NumberFormat::createInstance( |
+ icu::Locale::getDefault(), kind, status)); |
DCHECK(U_SUCCESS(status)); |
} |
std::unique_ptr<icu::NumberFormat> number_format; |
}; |
+struct NumberFormatWrapper : FormatWrapper { |
+ NumberFormatWrapper() { Reset(UNUM_DECIMAL); } |
+}; |
+ |
+struct PercentFormatWrapper : FormatWrapper { |
+ PercentFormatWrapper() { |
+ Reset(UNUM_PERCENT); |
+ number_format.get()->setMaximumFractionDigits(0); |
+ number_format.get()->setMinimumFractionDigits(0); |
+ } |
+}; |
+ |
LazyInstance<NumberFormatWrapper> g_number_format_int = |
LAZY_INSTANCE_INITIALIZER; |
LazyInstance<NumberFormatWrapper> g_number_format_float = |
LAZY_INSTANCE_INITIALIZER; |
+LazyInstance<PercentFormatWrapper> g_number_format_percent = |
+ LAZY_INSTANCE_INITIALIZER; |
} // namespace |
@@ -78,11 +89,26 @@ string16 FormatDouble(double number, int fractional_digits) { |
return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); |
} |
+string16 FormatPercent(int number) { |
+ icu::NumberFormat* percent_format = |
+ g_number_format_percent.Get().number_format.get(); |
+ |
Peter Kasting
2016/05/19 09:35:52
Nit: I'd remove this blank line and instead add on
Greg Levin
2016/05/19 15:42:13
Function rewritten, no blank lines.
|
+ if (!percent_format) { |
+ // As a fallback, just return the default percent formatting in a string. |
+ return UTF8ToUTF16(StringPrintf("%d%%", number)); |
jungshik at Google
2016/05/18 22:35:03
nit: ASCIIToUTF16()
Greg Levin
2016/05/19 15:42:13
Function no longer uses cached lazy instance, so n
|
+ } |
+ icu::UnicodeString ustr; |
+ percent_format->format(static_cast<double>(number) / 100.0, ustr); |
+ |
+ return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); |
+} |
+ |
namespace testing { |
void ResetFormatters() { |
- g_number_format_int.Get().Reset(); |
- g_number_format_float.Get().Reset(); |
+ g_number_format_int.Get().Reset(UNUM_DECIMAL); |
+ g_number_format_float.Get().Reset(UNUM_DECIMAL); |
+ g_number_format_percent.Get().Reset(UNUM_PERCENT); |
} |
} // namespace testing |