| 112 | } |
| 113 | |
| 114 | string HumanReadableNumBytes::ToString(int64 num_bytes) { |
| 115 | if (num_bytes == kint64min) { |
| 116 | // Special case for number with not representable nagation. |
| 117 | return "-8E"; |
| 118 | } |
| 119 | |
| 120 | const char *neg_str = GetNegStr(&num_bytes); |
| 121 | |
| 122 | // Special case for bytes. |
| 123 | if (num_bytes < GG_LONGLONG(1024)) { |
| 124 | // No fractions for bytes. |
| 125 | return StringPrintf("%s%" PRId64 "B", neg_str, num_bytes); |
| 126 | } |
| 127 | |
| 128 | static const char units[] = "KMGTPE"; // int64 only goes up to E. |
| 129 | const char* unit = units; |
| 130 | while (num_bytes >= GG_LONGLONG(1024) * GG_LONGLONG(1024)) { |
| 131 | num_bytes /= GG_LONGLONG(1024); |
| 132 | ++unit; |
| 133 | CHECK(unit < units + arraysize(units)); |
| 134 | } |
| 135 | |
| 136 | return StringPrintf(((*unit == 'K') |
| 137 | ? "%s%.1f%c" |
| 138 | : "%s%.2f%c"), neg_str, num_bytes / 1024.0, *unit); |
| 139 | } |
| 140 | |
| 141 | string HumanReadableNumBytes::ToStringWithoutRounding(int64 num_bytes) { |
| 142 | if (num_bytes == kint64min) { |
no test coverage detected