github.com/govalues/decimal (go): Package decimal implements immutable decimal floating-point numbers.  This packages is designed specifically for use in transactional financial systems. The amounts involved in financial transactions typically do not exceed 99,999,999,999,999,999.99, so uint64 is used to store decimal coefficients, which reduces heap allocations, lowers memory consumption, and improves performance.  The range of a decimal value depends on the size of its coefficient. Since the coefficient is stored as an uint64, a Decimal can have a maximum of 19 digits. Additionally, the range of the Decimal depends on its scale, which determines the number of decimal places. Here are some examples of ranges supported for frequently used scales:  Arithmetic operations in this package are based on General Decimal Arithmetic and usually involve two steps:  The operation is first performed using only uint64 variables. If no overflow occurs, the result is returned. If an overflow occurs, the operation proceeds to step 2.  The operation is performed again using big.Int variables. The result is rounded to fit into 19 digits. If no significant digits are lost during rounding, the result is returned. If significant digits are lost, an error is returned.  The purpose of the first step is to optimize the performance of arithmetic operations and reduce memory consumption. Since the coefficient is stored as an uint64, arithmetic operations using only uint64 variables can be performed quickly and efficiently. It is expected that most of the arithmetic operations will be successfully completed during the first step.  The following rules are used to determine the significance of digits:  To fit the results of arithmetic operations into 19 digits, the package uses half-to-even rounding, which ensures that rounding errors are evenly distributed between rounding up and rounding down.  In addition to implicit half-to-even rounding, the Decimal package provides several methods for explicit rounding:  Arithmetic operations return errors in the following cases:  Decimal overflow. This error occurs when significant digits are lost during rounding to fit 19 digits. This typically happens when dealing with large numbers or when you requested large number of digits after the decimal point to be considered signigicant. Refer to the supported ranges section, if your application needs to handle numbers that are close to the minimum or maximum values, this package may not be suitable. Consider using packages that store coefficients using big.Int type, such as ShopSpring Decimal or CockroachDB Decimal.  Division by zero. Unlike the standard library, this package does not panic when dividing by zero. Instead, it returns an error.  This example calculates an approximate value of pi using the Leibniz formula for pi. The Leibniz formula is an infinite series that converges to pi/4, and is given by the equation: 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... = pi/4. This example computes the series up to the 5000th term using decimal arithmetic and returns the approximate value of pi.   This example implements a simple calculator that evaluates mathematical expressions written in postfix (or reverse Polish) notation. The calculator can handle basic arithmetic operations such as addition, subtraction, multiplication, and division.
Keywords: decimal|financial|floating-point|general-decimal-arithmetic|go|golang|money