Skip to content

Latest commit

 

History

History
208 lines (148 loc) · 3.19 KB

2_float_and_string_datatypes.livemd

File metadata and controls

208 lines (148 loc) · 3.19 KB

Learning_Elixir

Built-in Data Types (Floating, Charlist & String)

Noting down the datatypes that need more effort to remember or not simple in its existence like float

Float

Here let's take an example float value 1200

Though 1200.0 is directly entered and while evaluating this, the system returns 1200.0 But the system stores this value in scientific form like this 1.2e3

ONLY when ":erlang.float_to_binary(1200.0, decimals: 2)" OR ":erlang.float_to_binary(1200.0, [{:decimals, 2}])"

The float value is stored as the same value as we stored

# Entering a float value
1200.0
1200.0
# to_string(float()) Returns a binary which corresponds to the shortest text representation of the given float.
Float.to_string(1200.0)
"1.2e3"
# Assigning a float value and printing it using IO.puts()
f = 1200.0
IO.puts(f)
1.2e3
:ok
# Keyword List method 
:erlang.float_to_binary(1200.0, decimals: 2)
"1200.00"
# Tuples with Atom
:erlang.float_to_binary(1200.0, [{:decimals, 2}])
"1200.00"

String

This section is for String Operations. Points to remember in Elixir

  1. Strings in Elixir are binaries
  2. enclosed in single quotes are '' are characters
  3. enclosed in single quotes are "" are strings
  4. Strings can be changed to Characters and vice versa
  5. Interpolation format in Elixir #{}
# In Elixir Characters represent Unicode code point
# Here, ~c"abcd" means the char list [97, 98, 99, 100], which corresponds to the Unicode code points of the lowercase letters 'a', 'b', 'c', and 'd'.
list_of_bytes = <<97, 98, 99, 100>>
"abcd"
list_of_bytes == "abcd"
true
string_1 = ~c"alphabets"
~c"alphabets"
string_2 = "alphabets"
"alphabets"
# Interpolation
name = "Min Yoongi"
"Hello #{name}"
"Hello Min Yoongi"
# List of code points created with single quotes ''
a = ~c"Climatic Crisis is real"
is_list(a)
true
# Converrting a char list to a string
a = ~c"hello this is jay"
Kernel.to_string(a)
"hello this is jay"
# Converrting a string to a char list
a = "sending good luck charm"
Kernel.to_charlist(a)
~c"sending good luck charm"
a = ~c"converting this to a string"
to_string(a)
"converting this to a string"
b = "converting this to a char list"
to_charlist(b)
~c"converting this to a char list"