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/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "third_party/icu/source/common/unicode/ustring.h" | 18 #include "third_party/icu/source/common/unicode/ustring.h" |
18 #include "third_party/icu/source/i18n/unicode/numfmt.h" | 19 #include "third_party/icu/source/i18n/unicode/numfmt.h" |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 | 22 |
(...skipping 25 matching lines...) Expand all Loading... |
47 LAZY_INSTANCE_INITIALIZER; | 48 LAZY_INSTANCE_INITIALIZER; |
48 | 49 |
49 } // namespace | 50 } // namespace |
50 | 51 |
51 string16 FormatNumber(int64_t number) { | 52 string16 FormatNumber(int64_t number) { |
52 icu::NumberFormat* number_format = | 53 icu::NumberFormat* number_format = |
53 g_number_format_int.Get().number_format.get(); | 54 g_number_format_int.Get().number_format.get(); |
54 | 55 |
55 if (!number_format) { | 56 if (!number_format) { |
56 // As a fallback, just return the raw number in a string. | 57 // As a fallback, just return the raw number in a string. |
57 return UTF8ToUTF16(StringPrintf("%" PRId64, number)); | 58 return ASCIIToUTF16(StringPrintf("%" PRId64, number)); |
58 } | 59 } |
59 icu::UnicodeString ustr; | 60 icu::UnicodeString ustr; |
60 number_format->format(number, ustr); | 61 number_format->format(number, ustr); |
61 | 62 |
62 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); | 63 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); |
63 } | 64 } |
64 | 65 |
65 string16 FormatDouble(double number, int fractional_digits) { | 66 string16 FormatDouble(double number, int fractional_digits) { |
66 icu::NumberFormat* number_format = | 67 icu::NumberFormat* number_format = |
67 g_number_format_float.Get().number_format.get(); | 68 g_number_format_float.Get().number_format.get(); |
68 | 69 |
69 if (!number_format) { | 70 if (!number_format) { |
70 // As a fallback, just return the raw number in a string. | 71 // As a fallback, just return the raw number in a string. |
71 return UTF8ToUTF16(StringPrintf("%f", number)); | 72 return ASCIIToUTF16(StringPrintf("%f", number)); |
72 } | 73 } |
73 number_format->setMaximumFractionDigits(fractional_digits); | 74 number_format->setMaximumFractionDigits(fractional_digits); |
74 number_format->setMinimumFractionDigits(fractional_digits); | 75 number_format->setMinimumFractionDigits(fractional_digits); |
75 icu::UnicodeString ustr; | 76 icu::UnicodeString ustr; |
76 number_format->format(number, ustr); | 77 number_format->format(number, ustr); |
77 | 78 |
78 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); | 79 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); |
79 } | 80 } |
80 | 81 |
| 82 string16 FormatPercent(int number) { |
| 83 return i18n::MessageFormatter::FormatWithNumberedArgs( |
| 84 ASCIIToUTF16("{0,number,percent}"), static_cast<double>(number) / 100.0); |
| 85 } |
| 86 |
81 namespace testing { | 87 namespace testing { |
82 | 88 |
83 void ResetFormatters() { | 89 void ResetFormatters() { |
84 g_number_format_int.Get().Reset(); | 90 g_number_format_int.Get().Reset(); |
85 g_number_format_float.Get().Reset(); | 91 g_number_format_float.Get().Reset(); |
86 } | 92 } |
87 | 93 |
88 } // namespace testing | 94 } // namespace testing |
89 | 95 |
90 } // namespace base | 96 } // namespace base |
OLD | NEW |