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