📄 numberformatter.cpp
字号:
//
// NumberFormatter.cpp
//
// $Id: //poco/Main/Foundation/src/NumberFormatter.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "Foundation/NumberFormatter.h"
#include <stdio.h>
#include <ctype.h>
#if defined(_MSC_VER)
#define I64_FMT "I64"
#elif defined(__APPLE__)
#define I64_FMT "q"
#else
#define I64_FMT "ll"
#endif
Foundation_BEGIN
std::string NumberFormatter::format(int value)
{
char buffer[64];
sprintf(buffer, "%d", value);
return std::string(buffer);
}
std::string NumberFormatter::format(int value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%*d", width, value);
return std::string(buffer);
}
std::string NumberFormatter::format0(int value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%0*d", width, value);
return std::string(buffer);
}
std::string NumberFormatter::formatHex(int value)
{
char buffer[64];
sprintf(buffer, "%X", value);
return std::string(buffer);
}
std::string NumberFormatter::formatHex(int value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%0*X", width, value);
return std::string(buffer);
}
std::string NumberFormatter::format(unsigned value)
{
char buffer[64];
sprintf(buffer, "%u", value);
return std::string(buffer);
}
std::string NumberFormatter::format(unsigned value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%*u", width, value);
return std::string(buffer);
}
std::string NumberFormatter::format0(unsigned int value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%0*u", width, value);
return std::string(buffer);
}
std::string NumberFormatter::formatHex(unsigned value)
{
char buffer[64];
sprintf(buffer, "%X", value);
return std::string(buffer);
}
std::string NumberFormatter::formatHex(unsigned value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%0*X", width, value);
return std::string(buffer);
}
std::string NumberFormatter::format(long value)
{
char buffer[64];
sprintf(buffer, "%ld", value);
return std::string(buffer);
}
std::string NumberFormatter::format(long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%*ld", width, value);
return std::string(buffer);
}
std::string NumberFormatter::format0(long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%0*ld", width, value);
return std::string(buffer);
}
std::string NumberFormatter::formatHex(long value)
{
char buffer[64];
sprintf(buffer, "%lX", value);
return std::string(buffer);
}
std::string NumberFormatter::formatHex(long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%0*lX", width, value);
return std::string(buffer);
}
std::string NumberFormatter::format(unsigned long value)
{
char buffer[64];
sprintf(buffer, "%lu", value);
return std::string(buffer);
}
std::string NumberFormatter::format(unsigned long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%*lu", width, value);
return std::string(buffer);
}
std::string NumberFormatter::format0(unsigned long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%0*lu", width, value);
return std::string(buffer);
}
std::string NumberFormatter::formatHex(unsigned long value)
{
char buffer[64];
sprintf(buffer, "%lX", value);
return std::string(buffer);
}
std::string NumberFormatter::formatHex(unsigned long value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%0*lX", width, value);
return std::string(buffer);
}
#if defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
std::string NumberFormatter::format(Int64 value)
{
char buffer[64];
sprintf(buffer, "%"I64_FMT"d", value);
return std::string(buffer);
}
std::string NumberFormatter::format(Int64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%*"I64_FMT"d", width, value);
return std::string(buffer);
}
std::string NumberFormatter::format0(Int64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%0*"I64_FMT"d", width, value);
return std::string(buffer);
}
std::string NumberFormatter::formatHex(Int64 value)
{
char buffer[64];
sprintf(buffer, "%"I64_FMT"X", value);
return std::string(buffer);
}
std::string NumberFormatter::formatHex(Int64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%0*"I64_FMT"X", width, value);
return std::string(buffer);
}
std::string NumberFormatter::format(UInt64 value)
{
char buffer[64];
sprintf(buffer, "%"I64_FMT"u", value);
return std::string(buffer);
}
std::string NumberFormatter::format(UInt64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%*"I64_FMT"u", width, value);
return std::string(buffer);
}
std::string NumberFormatter::format0(UInt64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%0*"I64_FMT"u", width, value);
return std::string(buffer);
}
std::string NumberFormatter::formatHex(UInt64 value)
{
char buffer[64];
sprintf(buffer, "%"I64_FMT"X", value);
return std::string(buffer);
}
std::string NumberFormatter::formatHex(UInt64 value, int width)
{
poco_assert (width > 0 && width < 64);
char buffer[64];
sprintf(buffer, "%0*"I64_FMT"X", width, value);
return std::string(buffer);
}
#endif // defined(POCO_HAVE_INT64) && !defined(POCO_LONG_IS_64_BIT)
std::string NumberFormatter::format(float value)
{
char buffer[64];
sprintf(buffer, "%.*g", 10, (double) value);
return std::string(buffer);
}
std::string NumberFormatter::format(double value)
{
char buffer[64];
sprintf(buffer, "%.*g", 20, value);
return std::string(buffer);
}
std::string NumberFormatter::format(double value, int precision)
{
poco_assert (precision >= 0 && precision < 32);
char buffer[64];
sprintf(buffer, "%.*f", precision, value);
return std::string(buffer);
}
std::string NumberFormatter::format(double value, int width, int precision)
{
poco_assert (width > 0 && width < 64 && precision >= 0 && precision < width);
char buffer[64];
sprintf(buffer, "%*.*f", width, precision, value);
return std::string(buffer);
}
std::string NumberFormatter::format(const void* ptr)
{
char buffer[24];
#if defined(POCO_PTR_IS_64_BIT)
sprintf(buffer, "%016"I64_FMT"X", (UIntPtr) ptr);
#else
sprintf(buffer, "%08lX", (UIntPtr) ptr);
#endif
return std::string(buffer);
}
Foundation_END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -