ClickHouse/src/Common/formatReadable.cpp

75 lines
2.3 KiB
C++
Raw Normal View History

#include <cmath>
#include <Common/formatReadable.h>
#include <IO/DoubleConverter.h>
#include <IO/WriteBufferFromString.h>
#include <IO/WriteHelpers.h>
2020-02-25 18:20:08 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER;
}
}
static void formatReadable(double size, DB::WriteBuffer & out, int precision, const char ** units, size_t units_size, double delimiter)
{
size_t i = 0;
for (; i + 1 < units_size && fabs(size) >= delimiter; ++i)
size /= delimiter;
DB::DoubleConverter<false>::BufferType buffer;
double_conversion::StringBuilder builder{buffer, sizeof(buffer)};
const auto result = DB::DoubleConverter<false>::instance().ToFixed(size, precision, &builder);
if (!result)
throw DB::Exception("Cannot print float or double number", DB::ErrorCodes::CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER);
out.write(buffer, builder.position());
writeCString(units[i], out);
}
void formatReadableSizeWithBinarySuffix(double value, DB::WriteBuffer & out, int precision)
{
const char * units[] = {" B", " KiB", " MiB", " GiB", " TiB", " PiB", " EiB", " ZiB", " YiB"};
formatReadable(value, out, precision, units, sizeof(units) / sizeof(units[0]), 1024);
}
std::string formatReadableSizeWithBinarySuffix(double value, int precision)
{
2017-07-31 21:39:24 +00:00
DB::WriteBufferFromOwnString out;
formatReadableSizeWithBinarySuffix(value, out, precision);
2017-07-31 21:39:24 +00:00
return out.str();
}
void formatReadableSizeWithDecimalSuffix(double value, DB::WriteBuffer & out, int precision)
{
const char * units[] = {" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"};
formatReadable(value, out, precision, units, sizeof(units) / sizeof(units[0]), 1000);
}
std::string formatReadableSizeWithDecimalSuffix(double value, int precision)
{
2017-07-31 21:39:24 +00:00
DB::WriteBufferFromOwnString out;
formatReadableSizeWithDecimalSuffix(value, out, precision);
2017-07-31 21:39:24 +00:00
return out.str();
}
void formatReadableQuantity(double value, DB::WriteBuffer & out, int precision)
{
const char * units[] = {"", " thousand", " million", " billion", " trillion", " quadrillion"};
formatReadable(value, out, precision, units, sizeof(units) / sizeof(units[0]), 1000);
}
std::string formatReadableQuantity(double value, int precision)
{
2017-07-31 21:39:24 +00:00
DB::WriteBufferFromOwnString out;
formatReadableQuantity(value, out, precision);
2017-07-31 21:39:24 +00:00
return out.str();
}