Index: base/i18n/number_formatting.cc |
diff --git a/base/i18n/number_formatting.cc b/base/i18n/number_formatting.cc |
index 6f454a0848bdb3aa7fe0c1d3f5aafbb04be58030..4fc5d7e4d6bbe817d181acb3834b909fe0fcc248 100644 |
--- a/base/i18n/number_formatting.cc |
+++ b/base/i18n/number_formatting.cc |
@@ -23,28 +23,35 @@ 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); } |
+}; |
+ |
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 +85,33 @@ string16 FormatDouble(double number, int fractional_digits) { |
return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); |
} |
+string16 FormatPercent(int number) { |
+ return FormatPercentFromDouble(static_cast<double>(number) / 100.0, 0); |
Greg Levin
2016/05/17 18:17:44
I don't totally understand why the NumberFormat ob
jungshik at Google
2016/05/17 23:58:42
Well, none of percent format used in our UI has fr
Greg Levin
2016/05/18 21:33:44
Sure. This function can just be
return i18n::Mes
|
+} |
+ |
+string16 FormatPercentFromDouble(double number, int fractional_digits) { |
+ icu::NumberFormat* percent_format = |
+ g_number_format_percent.Get().number_format.get(); |
+ |
+ if (!percent_format) { |
+ // As a fallback, just return the default percent formatting in a string. |
+ return UTF8ToUTF16( |
+ StringPrintf("%.*f%%", fractional_digits, number * 100.0)); |
+ } |
+ percent_format->setMaximumFractionDigits(fractional_digits); |
+ percent_format->setMinimumFractionDigits(fractional_digits); |
+ icu::UnicodeString ustr; |
+ percent_format->format(number, 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 |