Elixir基本类型 #

一、数值类型 #

1.1 整数 #

Elixir支持任意精度的整数:

elixir
iex(1)> 42
42

iex(2)> 123456789012345678901234567890
123456789012345678901234567890

进制表示 #

elixir
iex(1)> 0b1010
10

iex(2)> 0o755
493

iex(3)> 0xFF
255

iex(4)> 0x1F
31

下划线分隔 #

使用下划线提高可读性:

elixir
iex(1)> 1_000_000
1000000

iex(2)> 0xFF_FF
65535

类型检查 #

elixir
iex(1)> is_integer(42)
true

iex(2)> is_integer(42.0)
false

1.2 浮点数 #

遵循IEEE 754双精度标准:

elixir
iex(1)> 3.14
3.14

iex(2)> 1.0
1.0

iex(3)> 0.0
0.0

科学计数法 #

elixir
iex(1)> 1.0e10
1.0e10

iex(2)> 1.0e-10
1.0e-10

iex(3)> 6.022e23
6.022e23

类型检查 #

elixir
iex(1)> is_float(3.14)
true

iex(2)> is_float(3)
false

iex(3)> is_number(3)
true

iex(4)> is_number(3.14)
true

浮点数精度 #

elixir
iex(1)> 0.1 + 0.2
0.30000000000000004

1.3 数学运算 #

基本运算 #

elixir
iex(1)> 1 + 2
3

iex(2)> 5 - 3
2

iex(3)> 3 * 4
12

iex(4)> 10 / 2
5.0

整数除法 #

elixir
iex(1)> div(10, 3)
3

iex(2)> rem(10, 3)
1

iex(3)> Integer.floor_div(10, 3)
3

幂运算 #

elixir
iex(1)> :math.pow(2, 10)
1024.0

iex(2)> :math.sqrt(16)
4.0

iex(3)> :math.log(100)
4.605170185988092

iex(4)> :math.exp(1)
2.718281828459045

三角函数 #

elixir
iex(1)> :math.sin(0)
0.0

iex(2)> :math.cos(0)
1.0

iex(3)> :math.tan(0)
0.0

iex(4)> :math.pi()
3.141592653589793

取整与舍入 #

elixir
iex(1)> round(3.5)
4

iex(2)> round(3.4)
3

iex(3)> trunc(3.9)
3

iex(4)> Float.round(3.14159, 2)
3.14

二、原子 #

2.1 原子定义 #

原子是常量,其名称就是它的值:

elixir
iex(1)> :hello
:hello

iex(2)> :world
:world

iex(3)> :"hello world"
:"hello world"

2.2 特殊原子 #

Elixir有三个特殊的原子,它们是内置的:

elixir
iex(1)> true
true

iex(2)> false
false

iex(3)> nil
nil

iex(4)> is_atom(true)
true

iex(5)> is_atom(nil)
true

2.3 原子与布尔值 #

elixir
iex(1)> true == :true
true

iex(2)> false == :false
true

iex(3)> nil == :nil
true

2.4 原子命名规则 #

elixir
iex(1)> :hello
:hello

iex(2)> :hello_world
:hello_world

iex(3)> :helloWorld
:helloWorld

iex(4)> :"hello world"
:"hello world"

iex(5)> :"hello@world"
:"hello@world"

iex(6)> :+
:+

2.5 模块名作为原子 #

elixir
iex(1)> is_atom(String)
true

iex(2)> Atom.to_string(String)
"Elixir.String"

2.6 原子操作 #

elixir
iex(1)> Atom.to_string(:hello)
"hello"

iex(2)> String.to_atom("hello")
:hello

iex(3)> String.to_existing_atom("hello")
:hello

2.7 原子的使用场景 #

状态标识 #

elixir
def process(data) do
  case validate(data) do
    :ok -> handle_data(data)
    :error -> {:error, :invalid_data}
  end
end

模式匹配 #

elixir
def handle_response({:ok, data}), do: data
def handle_response({:error, reason}), do: {:error, reason}

键值对 #

elixir
%{name: "Alice", age: 30}

2.8 原子注意事项 #

原子不会被垃圾回收,不要动态创建大量原子:

elixir
String.to_existing_atom(user_input)

三、布尔值 #

3.1 布尔类型 #

elixir
iex(1)> true
true

iex(2)> false
false

iex(3)> is_boolean(true)
true

iex(4)> is_boolean(false)
true

3.2 逻辑运算 #

and/or/not #

严格布尔运算符,要求操作数必须是布尔值:

elixir
iex(1)> true and false
false

iex(2)> true or false
true

iex(3)> not true
false

iex(4)> true and 1
** (BadBooleanError) expected a boolean on left-side of "and", got: true

&&/||/! #

宽松运算符,可以处理任何值:

elixir
iex(1)> true && false
false

iex(2)> true || false
true

iex(3)> !true
false

iex(4)> nil && 1
nil

iex(5)> 1 && 2
2

iex(6)> false || 1
1

iex(7)> nil || 1
1

3.3 真值与假值 #

在Elixir中,只有 falsenil 被视为假值:

elixir
iex(1)> !!true
true

iex(2)> !!false
false

iex(3)> !!nil
false

iex(4)> !!0
true

iex(5)> !!""
true

iex(6)> !![]
true

3.4 比较运算符 #

elixir
iex(1)> 1 == 1
true

iex(2)> 1 != 2
true

iex(3)> 1 < 2
true

iex(4)> 1 <= 1
true

iex(5)> 1 > 0
true

iex(6)> 1 >= 1
true

3.5 严格比较 #

elixir
iex(1)> 1 == 1.0
true

iex(2)> 1 === 1.0
false

iex(3)> 1 != 1.0
false

iex(4)> 1 !== 1.0
true

四、类型比较 #

4.1 比较顺序 #

Elixir定义了类型比较的顺序:

text
number < atom < reference < function < port < pid < tuple < map < list < bitstring

示例:

elixir
iex(1)> 1 < :atom
true

iex(2)> :atom < [1, 2, 3]
true

iex(3)> [1, 2, 3] < "string"
true

4.2 类型检查函数 #

elixir
iex(1)> is_integer(1)
true

iex(2)> is_float(1.0)
true

iex(3)> is_number(1)
true

iex(4)> is_atom(:hello)
true

iex(5)> is_boolean(true)
true

iex(6)> is_nil(nil)
true

五、类型转换 #

5.1 数值转换 #

elixir
iex(1)> Integer.to_string(42)
"42"

iex(2)> Integer.to_string(42, 16)
"2A"

iex(3)> String.to_integer("42")
42

iex(4)> String.to_integer("FF", 16)
255

iex(5)> Float.to_string(3.14)
"3.14"

iex(6)> String.to_float("3.14")
3.14

5.2 原子转换 #

elixir
iex(1)> Atom.to_string(:hello)
"hello"

iex(2)> String.to_atom("hello")
:hello

iex(3)> String.to_existing_atom("hello")
:hello

5.3 通用转换 #

elixir
iex(1)> to_string(42)
"42"

iex(2)> to_string(:hello)
"hello"

iex(3)> to_string(3.14)
"3.14"

六、总结 #

本章学习了:

类型 示例 说明
整数 42, 0xFF, 0b1010 任意精度
浮点数 3.14, 1.0e10 IEEE 754双精度
原子 :hello, :ok, :error 常量标识符
布尔值 true, false 特殊原子

准备好学习字符串了吗?让我们进入下一章。

最后更新:2026-03-27