aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/gleam/int.gleam16
-rw-r--r--test/gleam/int_test.gleam14
3 files changed, 31 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b45b807..c9c9dfe 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@
- The `int` and `float` modules gain the `negate` function.
- The `result` module gains the `all` function.
- The `dynamic` module gains the `option` function.
+- The `int` module gains the `to_float` function.
## v0.11.0 - 2020-08-22
diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam
index 2dc4f46..f717eaa 100644
--- a/src/gleam/int.gleam
+++ b/src/gleam/int.gleam
@@ -42,6 +42,22 @@ pub external fn to_string(Int) -> String =
pub external fn to_base_string(Int, Int) -> String =
"erlang" "integer_to_binary"
+/// Takes an int and returns its value as a float
+///
+/// ## Examples
+///
+/// > to_float(5)
+/// 5.
+///
+/// > to_float(0)
+/// 0.
+///
+/// > to_float(-3)
+/// -3.
+///
+pub external fn to_float(a: Int) -> Float =
+ "erlang" "float"
+
/// Compares two ints, returning an order.
///
/// ## Examples
diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam
index 4a01073..97fbb84 100644
--- a/test/gleam/int_test.gleam
+++ b/test/gleam/int_test.gleam
@@ -52,6 +52,20 @@ pub fn to_base_string_test() {
|> should.equal("-64")
}
+pub fn to_float_test() {
+ int.to_float(1)
+ |> should.equal(1.)
+
+ int.to_float(5)
+ |> should.equal(5.)
+
+ int.to_float(0)
+ |> should.equal(0.)
+
+ int.to_float(-5)
+ |> should.equal(-5.)
+}
+
pub fn compare_test() {
int.compare(0, 0)
|> should.equal(order.Eq)