Decimal floating point has finite precision with arbitrarily large bounds. The purpose of this module is to support arithmetic using familiar "schoolhouse" rules and to avoid some of the tricky representation issues associated with binary floating point. The package is especially useful for financial applications or for contexts where users have expectations that are at odds with binary floating point (for instance, in binary floating point, 1.00 mod 0.1 gives 0.09999999999999995 instead of 0.0; Decimal.(of_string "1.00" mod of_string "0.1") returns the expected "0.00").
A total ordering function over the keys. This is a two-argument function f such that f e1 e2 is zero if the keys e1 and e2 are equal, f e1 e2 is strictly negative if e1 is smaller than e2, and f e1 e2 is strictly positive if e1 is greater than e2. Example: a suitable ordering function is the generic structural comparison function Stdlib.compare.
A hashing function on keys. It must be such that if two keys are equal according to equal, then they have identical hash values as computed by hash. Examples: suitable (equal, hash) pairs for arbitrary key types include
((=), hash) for comparing objects by structure (provided objects do not contain floats)
((fun x y -> compare x y = 0), hash) for comparing objects by structure and handling Stdlib.nan correctly
((==), hash) for comparing objects by physical equality (e.g. for mutable or cyclic objects).
of_string ?context str is str parsed into a decimal value with context (or the default context if none provided).
Note that a convenience syntax, e.g. 1.1m, is provided to write decimal literals in source code. See the readme for instructions on how to use it via the ppx_decimal PPX.
Sourceval of_yojson :
[> `Int of int| `Float of float| `String of string ]->(t, string)result
of_yojson json is the result of parsing a JSON value into a decimal:
integer is parsed
float is parsed with the usual caveat about float imprecision
of_float ?context float is the decimal representation of the float. This suffers from floating-point precision loss; the other constructors should be preferred.
alert lossy Suffers from floating-point precision loss. Other constructors should be preferred.
to_float ?context decimal is the float representation of the decimal. This suffers from floating-point precision loss; the other serializations should be preferred.
since 0.4.0
alert lossy Suffers from floating-point precision loss. Other serializations should be preferred.
adjusted t is the exponent of t after adjusting its coefficient (significand) into standard form, i.e. scientific notation.
E.g., Decimal.("314" |> of_string |> adjusted) is 2 because it is 3.14e2 in standard form. And, Decimal.("42e-10" |> of_string |> adjusted) is -9 because it is 4.2e-9 in standard form.
round ?n t is t rounded to the nearest integer, or to a given precision. If n is None, round t to the nearest integer. If t lies exactly halfway between two integers then it is rounded to the even integer.