6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Elixir Integer

Posted at

Elixir Integer

概要

Elixir の Integer について。

桁区切り

数値リテラルにアンダースコアを含めてもエラーにならないため
桁区切りに利用できます。Rubyと同じ。

iex> 1000000000
1000000000
iex> 100_0000_000
1000000000

基数

2進数は 0b
8進数は 0o
16進数は 0x
で記述します。

iex> 0b10
2
iex> 0o10
8
iex> 0x10
16
iex> 10
10

組み込み関数

下記を参照
http://elixir-lang.org/docs/stable/elixir/Integer.html

parse/1

binary を Integer に変換します

iex> Integer.parse("234")
{234, ""}
iex> Integer.parse("234.5")
{234, ".5"}
iex> Integer.parse("not number")

to_char_list/1

Integer を char_list に変換します

iex> Integer.to_char_list
'234'
iex> Integer.to_char_list(234_5)
'2345'

to_char_list/2

Integer を char_list に変換します。
第2引数で基数指定を行う。
基数は2-36までを指定可能。

iex> Integer.to_char_list(35, 2)
'100011'
iex> Integer.to_char_list(35, 8)
'43'
iex> Integer.to_char_list(35, 16)
'23'
iex> Integer.to_char_list(35, 36)
'Z'
iex> Integer.to_char_list(35, 10)
'35'

to_string/1

Integer を string に変換します

iex> Integer.to_string
"234"
iex> Integer.to_string(234_5)
"2345"

to_string/2

Integer を string に変換します。
第2引数で基数指定を行う。
基数は2-36までを指定可能。

iex> Integer.to_string(35, 2)
"100011"
iex> Integer.to_string(35, 8)
"43"
iex> Integer.to_string(35, 16)
"23"
iex> Integer.to_string(35, 36)
"Z"
iex> Integer.to_string(35, 10)
"35"

サンプル

サンプルコード

defmodule IntegerSamples do
  require Integer
  def exec do
    print_exec(Integer.is_odd(1))
    print_exec(Integer.is_odd(2))
    print_exec(Integer.is_even(1))
    print_exec(Integer.is_even(2))
  end

  defp print_exec(value) do
    IO.inspect value
  end
 end

IntegerSamples.exec

出力

true
false
false
true

参照

6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?