Elixir data types Part 2

calbertora

Carlos Ramírez

Posted on May 8, 2023

Elixir data types Part 2

Introduction

In the previous part, we learned about some basic data types in Elixir, such as atoms and booleans. In this part, we will explore some more data types that are commonly used in Elixir programs: integers, floats, and strings.

Integers/Floats

Integers and floats are numeric data types that represent whole numbers and decimal numbers respectively. They can be used for arithmetic operations, such as addition, subtraction, multiplication, and division. For example:

# Integers
iex> 1 + 2
3
iex> 10 - 5
5
iex> 3 * 4
12
iex> 20 / 4
5.0
Enter fullscreen mode Exit fullscreen mode
# Floats
iex> 1.5 + 2.5
4.0
iex> 3.14 - 1.0
2.14
iex> 2.0 * 1.5
3.0
iex> 10.0 / 3.0
3.3333333333333335
Enter fullscreen mode Exit fullscreen mode

Notice that dividing two integers always returns a float, even if the result is a whole number. This is because Elixir uses floating-point arithmetic to perform division, which can introduce some rounding errors. If you want to get an integer result from division, you can use the div/2 function instead:

iex> div(20, 4)
5
iex> div(10, 3)
3
Enter fullscreen mode Exit fullscreen mode

You can also use the rem/2 function to get the remainder of a division:

iex> rem(10, 3)
1
iex> rem(20, 4)
0
Enter fullscreen mode Exit fullscreen mode

Integers and floats can also be written in different formats, such as binary, octal, hexadecimal, scientific notation, and underscore separators. For example:

# Binary
iex> 0b1010 # equivalent to 10 in decimal
10

# Octal
iex> 0o12 # equivalent to 10 in decimal
10

# Hexadecimal
iex> 0xA # equivalent to 10 in decimal
10

# Scientific notation
iex> 1.0e3 # equivalent to 1000.0 in decimal
1000.0

# Underscore separators
iex> 1_000_000 # equivalent to 1000000 in decimal
1000000
Enter fullscreen mode Exit fullscreen mode

Strings

Strings are data types that represent sequences of characters enclosed in double quotes. They can be used for storing and manipulating text data, such as names, messages, and commands. For example:

# Strings
iex> "Hello"
"Hello"
iex> "World"
"World"
Enter fullscreen mode Exit fullscreen mode

Strings can be concatenated using the <> operator:

iex> "Hello" <> " " <> "World"
"Hello World"
Enter fullscreen mode Exit fullscreen mode

Strings can also be interpolated using the #{} syntax:

iex> name = "Alice"
"Alice"
iex> "Hello #{name}"
"Hello Alice"
Enter fullscreen mode Exit fullscreen mode

Strings are encoded using UTF-8, which means they can contain any Unicode character:

iex> "こんにちは"
"こんにちは"
iex> "👋"
"👋"
Enter fullscreen mode Exit fullscreen mode

Strings are also binaries, which means they are sequences of bytes that can be manipulated at a low level using binary pattern matching and functions from the Bitwise module. For example:

# Binary pattern matching on strings
iex> <<first::utf8, rest::binary>> = "Hello"
"Hello"
iex> first # the code point of the first character 'H'
72 
iex> rest # the rest of the string 'ello'
"ello"

# Bitwise operations on strings
iex> "Hello" &&& <<255>> # bitwise AND operation on each byte of the string with 255 
"Hello"
iex> "Hello" <<< 1 # bitwise left shift operation on each byte of the string by one bit 
<<136, 162, 162, 162, 176>>
Enter fullscreen mode Exit fullscreen mode

Summary

In this part, we learned about three more data types in Elixir: integers, floats, and strings. Integers and floats are numeric data types that can be used for arithmetic operations and can be written in different formats. Strings are sequences of characters that can be used for text data and can contain any Unicode character. Strings are also binaries that can be manipulated at a low level using binary pattern matching and bitwise operations.


Thanks for reading! My goal is to make Elixir accessible and easy to understand for everyone. If you have any questions or feedback, please don't hesitate to reach out. And be sure to check back regularly for more articles on https://dev.to/calbertora, as I strive to make each post as clear and readable as possible.

💖 💪 🙅 🚩
calbertora
Carlos Ramírez

Posted on May 8, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related