// A terabyte is:
// 1 TB = 1,099,511,627,776 bytes (binary, 2⁴⁰)
// 1 TB = 1,000,000,000,000 bytes (decimal, SI)
// Using constexpr
#include <iostream>
#include <format>
int main() {
// Define a terabyte constant (1 TiB = 2^40 bytes)
constexpr std::size_t TERABYTE = 1ULL << 40;
// 1. Standard Output (No commas)
std::cout << "TERABYTE (no commas): " << TERABYTE << " 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 << "TERABYTE (with imbue): " << TERABYTE << " bytes\n";
// 3. The std::format Approach (C++20)
// The ':L' tells format to use the locale provided
std::cout << "TERABYTE (with format): "
<< std::format(std::locale("en_US.UTF-8"), "{:L}", TERABYTE) << " 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:
TERABYTE (no commas): 1099511627776 bytes
TERABYTE (with imbue): 1,099,511,627,776 bytes
TERABYTE (with format): 1,099,511,627,776 bytes
*/