mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-18 05:32:52 +00:00
73 lines
2.3 KiB
C++
73 lines
2.3 KiB
C++
#include <cmath>
|
|
#include <sstream>
|
|
#include <iomanip>
|
|
|
|
#include <Common/formatReadable.h>
|
|
#include <IO/DoubleConverter.h>
|
|
#include <IO/WriteBufferFromString.h>
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
|
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)
|
|
{
|
|
std::string res;
|
|
DB::WriteBufferFromString out(res);
|
|
formatReadableSizeWithBinarySuffix(value, out, precision);
|
|
return res;
|
|
}
|
|
|
|
|
|
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)
|
|
{
|
|
std::string res;
|
|
DB::WriteBufferFromString out(res);
|
|
formatReadableSizeWithDecimalSuffix(value, out, precision);
|
|
return res;
|
|
}
|
|
|
|
|
|
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)
|
|
{
|
|
std::string res;
|
|
DB::WriteBufferFromString out(res);
|
|
formatReadableQuantity(value, out, precision);
|
|
return res;
|
|
}
|