{"id":13509297,"url":"https://github.com/ericmj/decimal","last_synced_at":"2025-05-13T22:00:14.749Z","repository":{"id":11891280,"uuid":"14453572","full_name":"ericmj/decimal","owner":"ericmj","description":"Arbitrary precision decimal arithmetic","archived":false,"fork":false,"pushed_at":"2025-02-11T20:53:59.000Z","size":480,"stargazers_count":469,"open_issues_count":4,"forks_count":103,"subscribers_count":11,"default_branch":"main","last_synced_at":"2025-05-07T11:02:52.726Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://hexdocs.pm/decimal/","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ericmj.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-11-16T19:48:44.000Z","updated_at":"2025-03-22T22:48:31.000Z","dependencies_parsed_at":"2024-02-29T09:28:15.982Z","dependency_job_id":"195e4966-a8ee-4071-acc2-cb3f78dc1673","html_url":"https://github.com/ericmj/decimal","commit_stats":{"total_commits":286,"total_committers":49,"mean_commits":5.836734693877551,"dds":0.465034965034965,"last_synced_commit":"1bed05bd2a3d0e0fb4242e3e8ecd3ba5859e5012"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericmj%2Fdecimal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericmj%2Fdecimal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericmj%2Fdecimal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ericmj%2Fdecimal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ericmj","download_url":"https://codeload.github.com/ericmj/decimal/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254036806,"owners_count":22003651,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T02:01:05.853Z","updated_at":"2025-05-13T22:00:14.250Z","avatar_url":"https://github.com/ericmj.png","language":"Elixir","funding_links":[],"categories":["Text and Numbers"],"sub_categories":[],"readme":"# Decimal\n\nArbitrary precision decimal arithmetic.\n\n## Concept\n\nDecimal represents values internally using three integers: a sign, a coefficient, and an exponent.\nIn this way, numbers of any size and with any number of decimal places can be represented exactly.\n\n```elixir\nDecimal.new(_sign = 1, _coefficient = 42, _exponent = 0) #=\u003e Decimal.new(\"42\")\nDecimal.new(-1, 42, 0) #=\u003e Decimal.new(\"-42\")\nDecimal.new(1, 42, -1) #=\u003e Decimal.new(\"4.2\")\nDecimal.new(1, 42, -20) #=\u003e Decimal.new(\"4.2E-19\")\nDecimal.new(1, 42, 20) #=\u003e Decimal.new(\"4.2E+21\")\nDecimal.new(1, 123456789987654321, -9) #=\u003e Decimal.new(\"123456789.987654321\")\n```\n\nFor calculations, the amount of desired precision - that is, the number of\ndecimal digits in the coefficient - can be specified.\n\n## Usage\n\nAdd Decimal as a dependency in your `mix.exs` file:\n\n```elixir\ndef deps do\n  [{:decimal, \"~\u003e 2.0\"}]\nend\n```\n\nNext, run `mix deps.get` in your shell to fetch and compile `Decimal`. Start an\ninteractive Elixir shell with `iex -S mix`:\n\n```elixir\niex\u003e alias Decimal, as: D\niex\u003e D.add(6, 7)\nDecimal.new(\"13\")\niex\u003e D.div(1, 3)\nDecimal.new(\"0.3333333333333333333333333333\")\niex\u003e D.new(\"0.33\")\nDecimal.new(\"0.33\")\n```\n\n## Examples\n\n### Using the context\n\nThe context specifies the maximum precision of the result of calculations and\nthe rounding algorithm if the result has a higher precision than the specified\nmaximum. It also holds the list of trap enablers and the current set\nflags.\n\nThe context is stored in the process dictionary. You don't have to pass the\ncontext around explicitly and the flags will be updated automatically.\n\nThe context is accessed with `Decimal.Context.get/0` and set with\n`Decimal.Context.set/1`. It can be set temporarily with\n`Decimal.Context.with/2`.\n\n```elixir\niex\u003e D.Context.get()\n%Decimal.Context{flags: [:rounded, :inexact], precision: 9, rounding: :half_up,\n traps: [:invalid_operation, :division_by_zero]}\n\niex\u003e D.Context.with(%D.Context{precision: 2}, fn -\u003e IO.inspect D.Context.get() end)\n%Decimal.Context{flags: [], precision: 2, rounding: :half_up,\n traps: [:invalid_operation, :division_by_zero]}\n%Decimal.Context{flags: [], precision: 2, rounding: :half_up,\n traps: [:invalid_operation, :division_by_zero]}\n\niex\u003e D.Context.set(%D.Context{D.Context.get() | traps: []})\n:ok\n\niex\u003e D.Context.get()\n%Decimal.Context{flags: [:rounded, :inexact], precision: 9, rounding: :half_up,\n traps: []}\n```\n\n### Precision and rounding\n\nUse `:precision` option to limit the amount of decimal digits in the\ncoefficient of any calculation result:\n\n```elixir\niex\u003e D.Context.set(%D.Context{D.Context.get() | precision: 9})\n:ok\n\niex\u003e D.div(100, 3)\nDecimal.new(\"33.3333333\")\n\niex\u003e D.Context.set(%D.Context{D.Context.get() | precision: 2})\n:ok\n\niex\u003e D.div(100, 3)\nDecimal.new(\"33\")\n```\n\nThe `:rounding` option specifies the algorithm and precision of the rounding\noperation:\n\n```elixir\niex\u003e D.Context.set(%D.Context{D.Context.get() | rounding: :half_up})\n:ok\n\niex\u003e D.div(31, 2)\nDecimal.new(\"16\")\n\niex\u003e D.Context.set(%D.Context{D.Context.get() | rounding: :floor})\n:ok\n\niex\u003e D.div(31, 2)\nDecimal.new(\"15\")\n```\n\n### Comparisons\n\nUsing comparison operators (`\u003c`, `=`, `\u003e`) with two or more decimal digits may\nnot produce accurate result. Instead, use comparison functions.\n\n```elixir\niex\u003e D.compare(-1, 0)\n:lt\niex\u003e D.compare(0, -1)\n:gt\niex\u003e D.compare(0, 0)\n:eq\n\niex\u003e D.equal?(-1, 0)\nfalse\niex\u003e D.equal?(0, \"0.0\")\ntrue\n```\n\n### Flags and trap enablers\n\nWhen an exceptional condition is signalled, its flag is set in the current\ncontext. `Decimal.Error` will be raised if the trap enabler is set.\n\n```elixir\niex\u003e D.Context.set(%D.Context{D.Context.get() | rounding: :floor, precision: 2})\n:ok\n\niex\u003e D.Context.get().traps\n[:invalid_operation, :division_by_zero]\n\niex\u003e D.Context.get().flags\n[]\n\niex\u003e D.div(31, 2)\nDecimal.new(\"15\")\n\niex\u003e D.Context.get().flags\n[:inexact, :rounded]\n```\n\n`:inexact` and `:rounded` flag were signalled above because the result of the\noperation was inexact given the context's precision and had to be rounded to\nfit the precision. `Decimal.Error` was not raised because the signals' trap\nenablers weren't set.\n\n```elixir\niex\u003e D.Context.set(%D.Context{D.Context.get() | traps: D.Context.get().traps ++ [:inexact]})\n:ok\n\niex\u003e D.div(31, 2)\n** (Decimal.Error)\n```\n\nThe default trap enablers, such as `:division_by_zero`, can be unset:\n\n```elixir\niex\u003e D.Context.get().traps\n[:invalid_operation, :division_by_zero]\n\niex\u003e D.div(42, 0)\n** (Decimal.Error)\n\niex\u003e D.Context.set(%D.Context{D.Context.get() | traps: [], flags: []})\n:ok\n\niex\u003e D.div(42, 0)\nDecimal.new(\"Infinity\")\n\niex\u003e D.Context.get().flags\n[:division_by_zero]\n```\n\n### Mitigating rounding errors\n\nTODO\n\n## License\n\n   Copyright 2013 Eric Meadows-Jönsson\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericmj%2Fdecimal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fericmj%2Fdecimal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fericmj%2Fdecimal/lists"}