mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-30 02:17:16 +02:00
docs: new formatting syntax ideas
This commit is contained in:
@ -100,8 +100,8 @@ performed without sacrificing accuracy. Please see the below example for a quick
|
||||
std::cout << std::setw(10) << std::setfill('*') << v2 << '\n'; // ***70 mi/h
|
||||
std::cout << std::format("{:*^10}\n", v3); // *110 km/h*
|
||||
std::println("{:%N in %U of %D}", v4); // 70 in mi/h of LT⁻¹
|
||||
std::println("{:{%N:.2f}%?%U}", v5); // 30.56 m/s
|
||||
std::println("{:{%N:.2f}%?{%U:dn}}", v6); // 31.29 m⋅s⁻¹
|
||||
std::println("{::N[.2f]}", v5); // 30.56 m/s
|
||||
std::println("{::N[.2f]U[dn]}", v6); // 31.29 m⋅s⁻¹
|
||||
std::println("{:%N}", v7); // 31
|
||||
}
|
||||
```
|
||||
@ -144,8 +144,8 @@ performed without sacrificing accuracy. Please see the below example for a quick
|
||||
std::cout << std::setw(10) << std::setfill('*') << v2 << '\n'; // ***70 mi/h
|
||||
std::cout << std::format("{:*^10}\n", v3); // *110 km/h*
|
||||
std::println("{:%N in %U of %D}", v4); // 70 in mi/h of LT⁻¹
|
||||
std::println("{:{%N:.2f}%?%U}", v5); // 30.56 m/s
|
||||
std::println("{:{%N:.2f}%?{%U:dn}}", v6); // 31.29 m⋅s⁻¹
|
||||
std::println("{::N[.2f]}", v5); // 30.56 m/s
|
||||
std::println("{::N[.2f]U[dn]}", v6); // 31.29 m⋅s⁻¹
|
||||
std::println("{:%N}", v7); // 31
|
||||
}
|
||||
```
|
||||
|
@ -38,7 +38,7 @@ The library source code is hosted on [GitHub](https://github.com/mpusz/mp-units)
|
||||
int main()
|
||||
{
|
||||
constexpr quantity dist = 364.4 * smoot;
|
||||
std::println("Harvard Bridge length = {:{%N:.5} %U} ({:{%N:.5} %U}, {:{%N:.5} %U}) ± 1 εar",
|
||||
std::println("Harvard Bridge length = {::N[.5]} ({::N[.5]}, {::N[.5]}) ± 1 εar",
|
||||
dist, dist.in(usc::foot), dist.in(si::metre));
|
||||
}
|
||||
```
|
||||
@ -58,7 +58,7 @@ The library source code is hosted on [GitHub](https://github.com/mpusz/mp-units)
|
||||
int main()
|
||||
{
|
||||
constexpr quantity dist = 364.4 * smoot;
|
||||
std::println("Harvard Bridge length = {:{%N:.5} %U} ({:{%N:.5} %U}, {:{%N:.5} %U}) ± 1 εar",
|
||||
std::println("Harvard Bridge length = {::N[.5]} ({::N[.5]}, {::N[.5]}) ± 1 εar",
|
||||
dist, dist.in(usc::foot), dist.in(si::metre));
|
||||
}
|
||||
```
|
||||
|
@ -61,7 +61,7 @@ constexpr auto speed_of_light_in_vacuum = 1 * si::si2019::speed_of_light_in_vacu
|
||||
|
||||
QuantityOf<isq::permittivity_of_vacuum> auto q = 1 / (permeability_of_vacuum * pow<2>(speed_of_light_in_vacuum));
|
||||
|
||||
std::println("permittivity of vacuum = {} = {:{%N:.3e} %U}", q, q.in(F / m));
|
||||
std::println("permittivity of vacuum = {} = {::N[.3e]}", q, q.in(F / m));
|
||||
```
|
||||
|
||||
The above first prints the following:
|
||||
|
@ -75,7 +75,7 @@ Here is a simple example showing how to deal with such quantities:
|
||||
const quantity duration = 2 * h;
|
||||
const quantity speed = avg_speed(distance, duration);
|
||||
|
||||
std::println("A car driving {} in {} has an average speed of {:{%N:.4} %U} ({:{%N:.4} %U})",
|
||||
std::println("A car driving {} in {} has an average speed of {::N[.4]} ({::N[.4]})",
|
||||
distance, duration, speed, speed.in(km / h));
|
||||
}
|
||||
```
|
||||
@ -103,7 +103,7 @@ Here is a simple example showing how to deal with such quantities:
|
||||
const quantity duration = 2 * h;
|
||||
const quantity speed = avg_speed(distance, duration);
|
||||
|
||||
std::println("A car driving {} in {} has an average speed of {:{%N:.4} %U} ({:{%N:.4} %U})",
|
||||
std::println("A car driving {} in {} has an average speed of {::N[.4]} ({::N[.4]})",
|
||||
distance, duration, speed, speed.in(km / h));
|
||||
}
|
||||
```
|
||||
@ -194,7 +194,7 @@ The previous example can be re-typed using typed quantities in the following way
|
||||
const quantity duration = isq::time(2 * h);
|
||||
const quantity speed = avg_speed(distance, duration);
|
||||
|
||||
std::println("A car driving {} in {} has an average speed of {:{%N:.4} %U} ({:{%N:.4} %U})",
|
||||
std::println("A car driving {} in {} has an average speed of {::N[.4]} ({::N[.4]})",
|
||||
distance, duration, speed, speed.in(km / h));
|
||||
}
|
||||
```
|
||||
@ -223,7 +223,7 @@ The previous example can be re-typed using typed quantities in the following way
|
||||
const quantity duration = isq::time(2 * h);
|
||||
const quantity speed = avg_speed(distance, duration);
|
||||
|
||||
std::println("A car driving {} in {} has an average speed of {:{%N:.4} %U} ({:{%N:.4} %U})",
|
||||
std::println("A car driving {} in {} has an average speed of {::N[.4]} ({::N[.4]})",
|
||||
distance, duration, speed, speed.in(km / h));
|
||||
}
|
||||
```
|
||||
|
@ -501,7 +501,7 @@ std::println("| {:<18} | {:^18} | {:^18} | {:^18} |",
|
||||
std::println("|{0:=^20}|{0:=^20}|{0:=^20}|{0:=^20}|", "");
|
||||
|
||||
auto print_temp = [&](std::string_view label, auto v) {
|
||||
std::println("| {:<18} | {:^18} | {:^18} | {:^18{%N:.2f}%?%U} |", label,
|
||||
std::println("| {:<14} | {:^18} | {:^18} | {:^18:N[.2f]} |", label,
|
||||
v - room_reference_temp, (v - si::ice_point).in(deg_C), (v - si::absolute_zero).in(deg_C));
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user