# format

***

**1. Formatting Strings with Formatting Flags**

```cpp
#include <iostream>

using namespace std;

int main() {
  int age = 30;
  string name = "John";
  
  // Left-align with 10 characters
  cout << left << setw(10) << name << endl;
  
  // Right-align with 10 characters
  cout << right << setw(10) << age << endl;
  
  // Zero-pad with 10 characters
  cout << setfill('0') << setw(10) << age << endl;
  
  // Show + sign for positive numbers
  cout << showpos << age << endl;
  
  // Show minus sign for negative numbers
  cout << noshowpos << -age << endl;
  
  return 0;
}
```

**2. Formatting Strings with Formatting Specifiers**

```cpp
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
  double pi = 3.14159265;
  
  // Fixed-point notation
  cout << fixed << setprecision(2) << pi << endl;
  
  // Scientific notation
  cout << scientific << setprecision(2) << pi << endl;
  
  // Octal notation
  cout << oct << pi << endl;
  
  // Hexadecimal notation
  cout << hex << pi << endl;
  
  return 0;
}
```

**3. Formatting Dates and Times**

```cpp
#include <iostream>
#include <ctime>
#include <iomanip>

using namespace std;

int main() {
  time_t now = time(0);
  
  // Format the current time
  cout << ctime(&now) << endl;
  
  // Format the current time using a specific format string
  cout << put_time(localtime(&now), "%a %b %d %H:%M:%S %Z %Y") << endl;
  
  return 0;
}
```

**4. Formatting Input and Output to Files**

```cpp
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main() {
  ofstream file("data.txt");
  
  // Format the output to the file
  file << fixed << setprecision(2);
  
  // Write some data to the file
  file << "John,30" << endl;
  
  file.close();
  
  return 0;
}
```

**5. Formatting Strings Using Boost.Format**

```cpp
#include <boost/format.hpp>

int main() {
  string name = "John";
  int age = 30;
  
  // Format the string using Boost.Format
  string message = boost::format("Name: %s, Age: %d") % name % age;
  
  cout << message << endl;
  
  return 0;
}
```

**6. Formatting Strings Using fmtlib**

```cpp
#include <fmt/core.h>

int main() {
  string name = "John";
  int age = 30;
  
  // Format the string using fmtlib
  string message = fmt::format("Name: {}, Age: {}", name, age);
  
  cout << message << endl;
  
  return 0;
}
```

**7. Formatting Numbers as Currency**

```cpp
#include <iostream>
#include <locale>
#include <iomanip>

using namespace std;

int main() {
  double price = 1234.56;
  
  // Format the number as currency
  cout << fixed << setprecision(2) << localeconv()->currency_symbol << price << endl;
  
  return 0;
}
```

**8. Formatting Numbers as Percentages**

```cpp
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
  double percentage = 0.987654;
  
  // Format the number as a percentage
  cout << fixed << setprecision(2) << percentage * 100 << "%" << endl;
  
  return 0;
}
```

**9. Formatting Strings with std::stringstream**

```cpp
#include <iostream>
#include <sstream>

using namespace std;

int main() {
  int age = 30;
  string name = "John";
  
  // Format the string using std::stringstream
  stringstream ss;
  ss << "Name: " << name << ", Age: " << age;
  
  string message = ss.str();
  
  cout << message << endl;
  
  return 0;
}
```

**10. Formatting Strings with std::to\_string**

```cpp
#include <iostream>
#include <sstream>

using namespace std;

int main() {
  int age = 30;
  string name = "John";
  
  // Format the string using std::to_string
  string message = "Name: " + name + ", Age: " + to_string(age);
  
  cout << message << endl;
  
  return 0;
}
```

**11. Formatting Strings with Boost.LexicalCast**

```cpp
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>

using namespace std;

int main() {
  int age = 30;
  string name = "John";
  
  // Format the string using Boost.LexicalCast and Boost.Format
  string message = boost::format("Name: %s, Age: %s") % name % boost::lexical_cast<string>(age);
  
  cout << message << endl;
  
  return 0;
}
```
