Fixed-point decimal data type for use in deterministic calculations, oftentimes involving money.
Template Parameters
Prec
The number of fractional digits
RoundPolicy
Specifies how to round in lossy operations
Decimal is internally represented as int64_t. It means that it can be passed around by value. It also means that operations with huge numbers can overflow and trap. For example, with Prec == 6, the maximum representable number is about 10 trillion.
Decimal should be serialized and stored as a string, NOT as double. Use Decimal{str} constructor (or Decimal::FromStringPermissive if rounding is allowed) to read a Decimal, and ToString(dec) (or ToStringTrailingZeros(dec)/ToStringFixed<N>(dec)) to write a Decimal.
Use arithmetic with caution! Multiplication and division operations involve rounding. You may want to cast to Decimal with another Prec or RoundPolicy beforehand. For that purpose you can use decimal64::decimal_cast<NewDec>(dec).
Usage example:
// create a single alias instead of specifying Decimal everywhere
The string must match the following regexp exactly:
No extra characters, including spaces, are allowed. Extra leading and trailing zeros (within Prec) are discarded. Input containing more fractional digits that Prec is not allowed (no implicit rounding).
The internal representation of Decimal is real_value * kDecimalFactor. Use for storing the value of Decimal efficiently when Prec is guaranteed not to change.
Operations with double, and even the returned value, is inexact. Prefer storing and sending Decimal as string, and performing the computations between Decimals.