docs: "Improved text output" added to the 2.2 release announcement

This commit is contained in:
Mateusz Pusz
2024-02-27 21:08:46 +01:00
parent 88e2656dc7
commit 1bc2ce585f

View File

@@ -13,10 +13,11 @@ categories:
[GitHub](https://github.com/mpusz/mp-units/releases/tag/v2.2.0) and
[Conan](https://conan.io/center/recipes/mp-units?version=2.2.0).**
Among other features, this release provides long-awaited support for C++20 modules, redesigns text
output formatting, and greatly simplifies quantity point usage. This post describes those and a few
other smaller interesting improvements, while a list of the most significant changes introduced by
the new version can be found in our [Release Notes](../../release_notes.md#2.2.0).
Among other features, this release provides long-awaited support for C++20 modules, redesigns and
enhances text output formatting, and greatly simplifies quantity point usage. This post describes
those and a few other smaller interesting improvements, while a much longer list of the most
significant changes introduced by the new version can be found in our
[Release Notes](../../release_notes.md#2.2.0).
<!-- more -->
@@ -164,6 +165,45 @@ With this release, the last one was renamed to `usc::zeroth_degree_Fahrenheit` t
named with its corresponding unit and with the `si::zeroth_degree_Celsius` (**breaking change**).
## Improved text output
With this release we can print not only whole quantities but also just their units or dimensions.
Also, we fixed the `std::format` support so users can now enjoy full C++20 compatibility and
don't have to use [fmtlib](https://github.com/fmtlib/fmt) anymore.
We have also changed the grammar for quantities formatting (**breaking change**). It introduces
the composition of underlying formatters that finally allows us to properly format user-defined
representation types (assuming they have `std::format` support). Additionally, thanks to a
new `%?` token we can provide a custom format string that will properly print quantity of any unit.
Here is a small preview of what is now available:
```cpp
using namespace mp_units::si::unit_symbols;
using namespace mp_units::international::unit_symbols;
quantity q = (90. * km / h).in(mph);
std::cout << "Number: " << q.numerical_value_in(mph) << "\n";
std::cout << "Unit: " << q.unit << "\n";
std::cout << "Dimension: " << q.dimension << "\n";
std::println("{:{%N:.2f}%?%U}", q);
std::println("{:.4f} in {} of {}", q.numerical_value_in(mph), q.unit, q.dimension);
std::println("{:{%N:.4f} in %U of %D}", q);
```
```text
Number: 55.9234
Unit: mi/h
Dimension: LT⁻¹
55.92 mi/h
55.9234 in mi/h of LT⁻¹
55.9234 in mi/h of LT⁻¹
```
More on this subject can be found in the updated [Text Output](../../users_guide/framework_basics/text_output.md)
chapter.
## Improved casts
We added a new conversion function. `value_cast<Unit, Representation>` forces the conversion