OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/i18n/number_formatting.h" | 5 #include "base/i18n/number_formatting.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/i18n/message_formatter.h" | 12 #include "base/i18n/message_formatter.h" |
| 13 #include "base/i18n/unicodestring.h" |
13 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
18 #include "third_party/icu/source/common/unicode/ustring.h" | 19 #include "third_party/icu/source/common/unicode/ustring.h" |
19 #include "third_party/icu/source/i18n/unicode/numfmt.h" | 20 #include "third_party/icu/source/i18n/unicode/numfmt.h" |
20 | 21 |
21 namespace base { | 22 namespace base { |
22 | 23 |
(...skipping 30 matching lines...) Expand all Loading... |
53 icu::NumberFormat* number_format = | 54 icu::NumberFormat* number_format = |
54 g_number_format_int.Get().number_format.get(); | 55 g_number_format_int.Get().number_format.get(); |
55 | 56 |
56 if (!number_format) { | 57 if (!number_format) { |
57 // As a fallback, just return the raw number in a string. | 58 // As a fallback, just return the raw number in a string. |
58 return ASCIIToUTF16(StringPrintf("%" PRId64, number)); | 59 return ASCIIToUTF16(StringPrintf("%" PRId64, number)); |
59 } | 60 } |
60 icu::UnicodeString ustr; | 61 icu::UnicodeString ustr; |
61 number_format->format(number, ustr); | 62 number_format->format(number, ustr); |
62 | 63 |
63 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); | 64 return i18n::UnicodeStringToString16(ustr); |
64 } | 65 } |
65 | 66 |
66 string16 FormatDouble(double number, int fractional_digits) { | 67 string16 FormatDouble(double number, int fractional_digits) { |
67 icu::NumberFormat* number_format = | 68 icu::NumberFormat* number_format = |
68 g_number_format_float.Get().number_format.get(); | 69 g_number_format_float.Get().number_format.get(); |
69 | 70 |
70 if (!number_format) { | 71 if (!number_format) { |
71 // As a fallback, just return the raw number in a string. | 72 // As a fallback, just return the raw number in a string. |
72 return ASCIIToUTF16(StringPrintf("%f", number)); | 73 return ASCIIToUTF16(StringPrintf("%f", number)); |
73 } | 74 } |
74 number_format->setMaximumFractionDigits(fractional_digits); | 75 number_format->setMaximumFractionDigits(fractional_digits); |
75 number_format->setMinimumFractionDigits(fractional_digits); | 76 number_format->setMinimumFractionDigits(fractional_digits); |
76 icu::UnicodeString ustr; | 77 icu::UnicodeString ustr; |
77 number_format->format(number, ustr); | 78 number_format->format(number, ustr); |
78 | 79 |
79 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); | 80 return i18n::UnicodeStringToString16(ustr); |
80 } | 81 } |
81 | 82 |
82 string16 FormatPercent(int number) { | 83 string16 FormatPercent(int number) { |
83 return i18n::MessageFormatter::FormatWithNumberedArgs( | 84 return i18n::MessageFormatter::FormatWithNumberedArgs( |
84 ASCIIToUTF16("{0,number,percent}"), static_cast<double>(number) / 100.0); | 85 ASCIIToUTF16("{0,number,percent}"), static_cast<double>(number) / 100.0); |
85 } | 86 } |
86 | 87 |
87 namespace testing { | 88 namespace testing { |
88 | 89 |
89 void ResetFormatters() { | 90 void ResetFormatters() { |
90 g_number_format_int.Get().Reset(); | 91 g_number_format_int.Get().Reset(); |
91 g_number_format_float.Get().Reset(); | 92 g_number_format_float.Get().Reset(); |
92 } | 93 } |
93 | 94 |
94 } // namespace testing | 95 } // namespace testing |
95 | 96 |
96 } // namespace base | 97 } // namespace base |
OLD | NEW |