Roll src/third_party/skia 8553396:c64137c
[chromium-blink-merge.git] / base / i18n / number_formatting.cc
blob47aa14cab23bc75fa42d8e9b00d85a4af361649d
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/i18n/number_formatting.h"
7 #include "base/format_macros.h"
8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "third_party/icu/source/common/unicode/ustring.h"
15 #include "third_party/icu/source/i18n/unicode/numfmt.h"
17 namespace base {
19 namespace {
21 // A simple wrapper around icu::NumberFormat that allows for resetting it
22 // (as LazyInstance does not).
23 struct NumberFormatWrapper {
24 NumberFormatWrapper() {
25 Reset();
28 void Reset() {
29 // There's no ICU call to destroy a NumberFormat object other than
30 // operator delete, so use the default Delete, which calls operator delete.
31 // This can cause problems if a different allocator is used by this file
32 // than by ICU.
33 UErrorCode status = U_ZERO_ERROR;
34 number_format.reset(icu::NumberFormat::createInstance(status));
35 DCHECK(U_SUCCESS(status));
38 scoped_ptr<icu::NumberFormat> number_format;
41 LazyInstance<NumberFormatWrapper> g_number_format_int =
42 LAZY_INSTANCE_INITIALIZER;
43 LazyInstance<NumberFormatWrapper> g_number_format_float =
44 LAZY_INSTANCE_INITIALIZER;
46 } // namespace
48 string16 FormatNumber(int64 number) {
49 icu::NumberFormat* number_format =
50 g_number_format_int.Get().number_format.get();
52 if (!number_format) {
53 // As a fallback, just return the raw number in a string.
54 return UTF8ToUTF16(StringPrintf("%" PRId64, number));
56 icu::UnicodeString ustr;
57 number_format->format(number, ustr);
59 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length()));
62 string16 FormatDouble(double number, int fractional_digits) {
63 icu::NumberFormat* number_format =
64 g_number_format_float.Get().number_format.get();
66 if (!number_format) {
67 // As a fallback, just return the raw number in a string.
68 return UTF8ToUTF16(StringPrintf("%f", number));
70 number_format->setMaximumFractionDigits(fractional_digits);
71 number_format->setMinimumFractionDigits(fractional_digits);
72 icu::UnicodeString ustr;
73 number_format->format(number, ustr);
75 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length()));
78 namespace testing {
80 void ResetFormatters() {
81 g_number_format_int.Get().Reset();
82 g_number_format_float.Get().Reset();
85 } // namespace testing
87 } // namespace base