addstar
How to set default decimal precision?
Noob question, how do I set the default decimal precision in phoenix?
I can see from the Decimal docs I need to run D.Context.set(%D.Context{D.Context.get() | precision: 48}) but where do I set this so when I run mix phx.server or iex -S mix it’s already set?
Most Liked
al2o3cr
Decimal.Context.get/set are a trivial wrapper over an entry in the process dictionary:
https://github.com/ericmj/decimal/blob/68633fc6651456b80cfee349ef96be0000449468/lib/decimal/context.ex#L101-L115
so AFAIK there’s not a “global” place to change them.
al2o3cr
The flipside of setting the context being really cheap: it’s fine to do it frequently, close to the code that depends on specific settings.
Setting it close to the calculation also avoids gotchas where code runs in another process unexpectedly; for instance, Cachex.fetch runs the supplied fallback function in an internal process, NOT the caller’s process.
kip
There is no global place to set it, it needs to be set in each process where you want it to be in affect. So if you’re talking about a Phoenix app (which you are I see from above), then you’ll need to set it in each request (as a plug would make sense). And then in each other process like liveview processes.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








