Info
-
Did you know that C++26 changed arithmetic overloads of std::to_string and std::to_wstring to use std::format?
Example
int main() {
setlocale(LC_ALL, "C");
std::cout << std::to_string(42); // prints 42
std::cout << std::to_string(.42); // prints 0.42
std::cout << std::to_string(-1e7); // prints -1e+07
}
Puzzle
- Can you add required
locale
to match expectations?
int main() {
using namespace boost::ut;
using std::literals::string_literals::operator""s;
"locale.to_string (all.us)"_test = [] {
// TODO
expect("-1e+07"s == std::to_string(-1e7));
};
"locale.to_string (numeric.eu)"_test = [] {
// TODO
expect("1,23"s == std::to_string(1.23));
};
}
Solutions
int main() {
using namespace boost::ut;
using std::literals::string_literals::operator""s;
"locale.to_string (all.us)"_test = [] {
std::setlocale(LC_ALL, "C");
expect("-1e+07"s == std::to_string(-1e7));
};
"locale.to_string (numeric.eu)"_test = [] {
std::setlocale(LC_NUMERIC, "de_DE.UTF-8");
expect("1.23"s == std::to_string(1.23)); // env dependent, should be 1,23
};
}