'Can't mix specification with fmt library

With fmt library (or std::format) I can do formatting like in the example below

#include <fmt/core.h>

int main() {
    double x = 2.0/3.0;
    fmt::print("{:*^30}\n", x); //print ******0.6666666666666666******
    fmt::print("{:+.3e}\n", x); //print +6.667e-01
 }

In the above code, I can:

  • Use the first formatting to center x and pad with *.
  • Use the second formatting to format x in scientific notation with 3 decimal places and a +.

How can I combine both of these to get this?

**********+6.667e-01**********


Solution 1:[1]

The standard format specification for basic types and strings is:

fill-and-align(optional) sign(optional) #(optional) 0(optional) width(optional) precision(optional) L(optional) type(optional)

This would lead me to think that your desired format specification is

fmt::print("{:*^+30.3e}\n", x);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 j6t