47816 {
47817
47818 if (numeric.size() <= 5) {
47819
47820 return string();
47821 }
47822
47823 idx_t number = 0;
47824 bool negative = false;
47825 idx_t i = 0;
47826 if (numeric[0] == '-') {
47827 negative = true;
47828 i++;
47829 }
47830 for (; i < numeric.size(); i++) {
47831 char c = numeric[i];
47832 if (c == '.') {
47833 break;
47834 }
47835 if (c < '0' || c > '9') {
47836
47837
47838 return string();
47839 }
47840 if (number >= 1000000000000000000ULL) {
47841
47842 return string();
47843 }
47844 number = number * 10 + static_cast<idx_t>(c - '0');
47845 }
47846 struct UnitBase {
47847 idx_t base;
47848 const char *name;
47849 };
47850 static constexpr idx_t BASE_COUNT = 5;
47851 UnitBase bases[] = {{1000000ULL, "million"},
47852 {1000000000ULL, "billion"},
47853 {1000000000000ULL, "trillion"},
47854 {1000000000000000ULL, "quadrillion"},
47855 {1000000000000000000ULL, "quintillion"}};
47856 idx_t base = 0;
47857 string unit;
47858 for (idx_t i = 0; i < BASE_COUNT; i++) {
47859
47860 idx_t rounded_number = number + ((bases[i].base / 100ULL) / 2);
47861 if (rounded_number >= bases[i].base) {
47862 base = bases[i].base;
47863 unit = bases[i].name;
47864 }
47865 }
47866 if (unit.empty()) {
47867 return string();
47868 }
47869 number += (base / 100ULL) / 2;
47870 idx_t decimal_unit = number / (base / 100ULL);
47871 string decimal_str = to_string(decimal_unit);
47872 string result;
47873 if (negative) {
47874 result += "-";
47875 }
47876 result += decimal_str.substr(0, decimal_str.size() - 2);
47877 result += decimal_sep == '\0' ? '.' : decimal_sep;
47878 result += decimal_str.substr(decimal_str.size() - 2, 2);
47879 result += " ";
47880 result += unit;
47881 return result;
47882}