1#include <cmath>
2#include <sstream>
3#include <iomanip>
4
5#include <Common/formatReadable.h>
6#include <IO/DoubleConverter.h>
7#include <IO/WriteBufferFromString.h>
8#include <IO/WriteHelpers.h>
9
10
11static void formatReadable(double size, DB::WriteBuffer & out, int precision, const char ** units, size_t units_size, double delimiter)
12{
13 size_t i = 0;
14 for (; i + 1 < units_size && fabs(size) >= delimiter; ++i)
15 size /= delimiter;
16
17 DB::DoubleConverter<false>::BufferType buffer;
18 double_conversion::StringBuilder builder{buffer, sizeof(buffer)};
19
20 const auto result = DB::DoubleConverter<false>::instance().ToFixed(size, precision, &builder);
21
22 if (!result)
23 throw DB::Exception("Cannot print float or double number", DB::ErrorCodes::CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER);
24
25 out.write(buffer, builder.position());
26 writeCString(units[i], out);
27}
28
29
30void formatReadableSizeWithBinarySuffix(double value, DB::WriteBuffer & out, int precision)
31{
32 const char * units[] = {" B", " KiB", " MiB", " GiB", " TiB", " PiB", " EiB", " ZiB", " YiB"};
33 formatReadable(value, out, precision, units, sizeof(units) / sizeof(units[0]), 1024);
34}
35
36std::string formatReadableSizeWithBinarySuffix(double value, int precision)
37{
38 DB::WriteBufferFromOwnString out;
39 formatReadableSizeWithBinarySuffix(value, out, precision);
40 return out.str();
41}
42
43
44void formatReadableSizeWithDecimalSuffix(double value, DB::WriteBuffer & out, int precision)
45{
46 const char * units[] = {" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"};
47 formatReadable(value, out, precision, units, sizeof(units) / sizeof(units[0]), 1000);
48}
49
50std::string formatReadableSizeWithDecimalSuffix(double value, int precision)
51{
52 DB::WriteBufferFromOwnString out;
53 formatReadableSizeWithDecimalSuffix(value, out, precision);
54 return out.str();
55}
56
57
58void formatReadableQuantity(double value, DB::WriteBuffer & out, int precision)
59{
60 const char * units[] = {"", " thousand", " million", " billion", " trillion", " quadrillion"};
61 formatReadable(value, out, precision, units, sizeof(units) / sizeof(units[0]), 1000);
62}
63
64std::string formatReadableQuantity(double value, int precision)
65{
66 DB::WriteBufferFromOwnString out;
67 formatReadableQuantity(value, out, precision);
68 return out.str();
69}
70