#include <iostream>
#include <locale>
#include <format>
// A petabyte (binary) is:
// 1 PiB = 2⁵⁰ bytes = 1,125,899,906,842,624 bytes
// A petabyte (decimal) is:
// 1 PB = 1,000,000,000,000,000 bytes
constexpr std::size_t PETABYTE = 1ULL << 50; // 1 PiB
constexpr std::size_t PETABYTE_DEC = 1'000'000'000'000'000ULL; // 1 PB
int main() {
std::size_t petabyte = PETABYTE;
// 1. Standard Output (No commas)
std::cout << "PETABYTE (no commas): " << petabyte << " bytes\n";
// 2. The Locale Approach
// This "imbues" the stream so it automatically formats numbers
try {
// Use "" to grab the user's system locale, or "en_US.UTF-8" for specific US formatting
std::cout.imbue(std::locale("en_US.UTF-8"));
std::cout << "PETABYTE (with imbue): " << petabyte << " bytes\n";
// 3. The std::format Approach (C++20)
// The ':L' tells format to use the locale provided
std::cout << "PETABYTE (with format): "
<< std::format(std::locale("en_US.UTF-8"), "{:L}", petabyte) << " bytes\n";
} catch (const std::runtime_error& e) {
// Fallback if the specific locale string isn't installed on the system
std::cerr << "Locale not supported: " << e.what() << std::endl;
}
}
/*
run:
PETABYTE (no commas): 1125899906842624 bytes
PETABYTE (with imbue): 1,125,899,906,842,624 bytes
PETABYTE (with format): 1,125,899,906,842,624 bytes
*/