MCPcopy Create free account
hub / github.com/chen3feng/toft / NumberToHumanReadableString

Function NumberToHumanReadableString

base/string/number.cpp:872–902  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

870
871template <typename T, int Base>
872std::string NumberToHumanReadableString(
873 T number,
874 const char* const*prefixes,
875 const char* unit,
876 int min_shift,
877 int max_shift
878 )
879{
880 bool neg = number < 0;
881 double n = fabs(static_cast<double>(number));
882
883 int shift;
884 GetMantissaAndShift<Base>(n, min_shift, max_shift, &n, &shift);
885
886 const char* sep = "";
887 if (unit[0] == ' ')
888 {
889 ++unit;
890 // ignore unit if it is " " and prefix is unnecessary
891 if (shift != 0 || unit[0] != '\0')
892 sep = " ";
893 }
894
895 char buffer[16];
896 int length = sprintf(buffer, "%s%.*g%s%s", // NOLINT(runtime/printf)
897 neg ? "-": "", n < 1000 ? 3 : 4, n, sep,
898 prefixes[shift]);
899 std::string result(buffer, length);
900 result += unit;
901 return result;
902}
903
904} // namespace
905

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected