chensan
Float.parse "9999999999999999"
iex(4)> Float.parse "9999999999999999"
{1.0e16, ""}
So if I want to get 9999999999999999, what should I do?
Marked As Solved
LostKobrakai
Not use Floats. Floats are just an approximation, not a correct number. Which is why one should never use floats for money values. You could either use integers or decimal numbers.
Also Liked
NobbZ
Why not Integer?
Decimals have some computational and memory overhead, because they are structs that consist of three values: The hole number part, the fraction part and the precision. For all computations made on Decimal they have to be weighed against each other.
If you do not expect fractions to occur, use an Integer, if you expect fractions to occur but you do also know that they will always have the same precision, use a tagged Integer (eg. {:money, 1000} for 10$) and write some functions to use it.
rvirding
You can find a number of good articles on the net describing floats and the problems with them. For example:
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
- #code-sync
- #javascript
- #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









