Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 UErrorCode status = U_ZERO_ERROR; | 27 UErrorCode status = U_ZERO_ERROR; |
| 28 number_format.reset(icu::NumberFormat::createInstance(status)); | 28 number_format.reset(icu::NumberFormat::createInstance(status)); |
| 29 DCHECK(U_SUCCESS(status)); | 29 DCHECK(U_SUCCESS(status)); |
| 30 } | 30 } |
| 31 | 31 |
| 32 scoped_ptr<icu::NumberFormat> number_format; | 32 scoped_ptr<icu::NumberFormat> number_format; |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 } // namespace | 35 } // namespace |
| 36 | 36 |
| 37 static LazyInstance<NumberFormatWrapper> g_number_format(LINKER_INITIALIZED); | 37 static |
| 38 LazyInstance<NumberFormatWrapper> g_number_format_int(LINKER_INITIALIZED); | |
| 39 | |
| 40 static | |
| 41 LazyInstance<NumberFormatWrapper> g_number_format_float(LINKER_INITIALIZED); | |
| 38 | 42 |
| 39 string16 FormatNumber(int64 number) { | 43 string16 FormatNumber(int64 number) { |
| 40 icu::NumberFormat* number_format = g_number_format.Get().number_format.get(); | 44 icu::NumberFormat* number_format = |
| 45 g_number_format_int.Get().number_format.get(); | |
|
Evan Stade
2011/06/21 03:18:41
this is the best line of code ever
Avi (use Gerrit)
2011/06/21 14:30:19
You're welcome. I'm here all week.
| |
| 41 | 46 |
| 42 if (!number_format) { | 47 if (!number_format) { |
| 43 // As a fallback, just return the raw number in a string. | 48 // As a fallback, just return the raw number in a string. |
| 44 return UTF8ToUTF16(StringPrintf("%" PRId64, number)); | 49 return UTF8ToUTF16(StringPrintf("%" PRId64, number)); |
| 45 } | 50 } |
| 46 icu::UnicodeString ustr; | 51 icu::UnicodeString ustr; |
| 47 number_format->format(number, ustr); | 52 number_format->format(number, ustr); |
| 48 | 53 |
| 49 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); | 54 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); |
| 50 } | 55 } |
| 51 | 56 |
| 57 string16 FormatDouble(double number, int fractional_digits) { | |
| 58 icu::NumberFormat* number_format = | |
| 59 g_number_format_float.Get().number_format.get(); | |
| 60 | |
| 61 if (!number_format) { | |
| 62 // As a fallback, just return the raw number in a string. | |
| 63 return UTF8ToUTF16(StringPrintf("%f", number)); | |
| 64 } | |
| 65 number_format->setMaximumFractionDigits(fractional_digits); | |
| 66 number_format->setMinimumFractionDigits(fractional_digits); | |
| 67 icu::UnicodeString ustr; | |
| 68 number_format->format(number, ustr); | |
| 69 | |
| 70 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); | |
| 71 } | |
| 72 | |
| 52 } // namespace base | 73 } // namespace base |
| OLD | NEW |