blob: b2eeb16ad9e80c11ed3cf75c157a0edbf225c5ab (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/i18n/number_formatting.h"
#include "base/format_macros.h"
#include "base/logging.h"
#include "base/lazy_instance.h"
#include "base/memory/scoped_ptr.h"
#include "base/stringprintf.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "unicode/numfmt.h"
#include "unicode/ustring.h"
namespace base {
namespace {
struct NumberFormatWrapper {
NumberFormatWrapper() {
Reset();
}
void Reset() {
// 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));
DCHECK(U_SUCCESS(status));
}
scoped_ptr<icu::NumberFormat> number_format;
};
LazyInstance<NumberFormatWrapper> g_number_format_int(LINKER_INITIALIZED);
LazyInstance<NumberFormatWrapper> g_number_format_float(LINKER_INITIALIZED);
} // namespace
string16 FormatNumber(int64 number) {
icu::NumberFormat* number_format =
g_number_format_int.Get().number_format.get();
if (!number_format) {
// As a fallback, just return the raw number in a string.
return UTF8ToUTF16(StringPrintf("%" PRId64, number));
}
icu::UnicodeString ustr;
number_format->format(number, ustr);
return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length()));
}
string16 FormatDouble(double number, int fractional_digits) {
icu::NumberFormat* number_format =
g_number_format_float.Get().number_format.get();
if (!number_format) {
// As a fallback, just return the raw number in a string.
return UTF8ToUTF16(StringPrintf("%f", number));
}
number_format->setMaximumFractionDigits(fractional_digits);
number_format->setMinimumFractionDigits(fractional_digits);
icu::UnicodeString ustr;
number_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();
}
} // namespace testing
} // namespace base
|