1// Copyright (c) 2008 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. 4 5#include "base/i18n/number_formatting.h" 6 7#include "base/format_macros.h" 8#include "base/logging.h" 9#include "base/singleton.h" 10#include "base/string_util.h" 11#include "base/utf_string_conversions.h" 12#include "unicode/numfmt.h" 13#include "unicode/ustring.h" 14 15namespace base { 16 17namespace { 18 19struct NumberFormatSingletonTraits 20 : public DefaultSingletonTraits<icu::NumberFormat> { 21 static icu::NumberFormat* New() { 22 UErrorCode status = U_ZERO_ERROR; 23 icu::NumberFormat* formatter = icu::NumberFormat::createInstance(status); 24 DCHECK(U_SUCCESS(status)); 25 return formatter; 26 } 27 // There's no ICU call to destroy a NumberFormat object other than 28 // operator delete, so use the default Delete, which calls operator delete. 29 // This can cause problems if a different allocator is used by this file than 30 // by ICU. 31}; 32 33} // namespace 34 35string16 FormatNumber(int64 number) { 36 icu::NumberFormat* number_format = 37 Singleton<icu::NumberFormat, NumberFormatSingletonTraits>::get(); 38 39 if (!number_format) { 40 // As a fallback, just return the raw number in a string. 41 return UTF8ToUTF16(StringPrintf("%" PRId64, number)); 42 } 43 icu::UnicodeString ustr; 44 number_format->format(number, ustr); 45 46 return string16(ustr.getBuffer(), static_cast<size_t>(ustr.length())); 47} 48 49} // namespace base 50