links: Elixir MOC


About Floating point numbers

Floats are numbers with one or more digits behind the decimal separator. They use the 64-bit double-precision floating-point format

float = 3.45
# => 3.45

Working with numbers

In the Integer and Float modules, you can find some useful functions for working with those types. Basic arithmetic operators are defined in the Kernel module

Conversion

Integers and Floats can be mixed in a single arithmetic expression. Using a float in an expression ensures the result will be a float too

2 * 3 
# => 6 
 
2 * 3.0 
# => 6.0

However, when doing division, the result will always be a float, even if integers are only used

6 / 2
# => 3.0

To convert a float to an integer you can discard the decimal part with trunc/1


tags: elixir floating-point

sources: