aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2019-04-22 22:53:10 +0000
committerLouis Pilfold <louis@lpil.uk>2019-04-22 22:54:26 +0000
commit8797d27e0801d37a6082ce54dac9f9d5cb01ec6c (patch)
treebb63c54be91b68aac6058c466d52634736549d4b
parent63d1ef2e449262becd654a13d1cb1462590e203f (diff)
downloadgleam_stdlib-8797d27e0801d37a6082ce54dac9f9d5cb01ec6c.tar.gz
gleam_stdlib-8797d27e0801d37a6082ce54dac9f9d5cb01ec6c.zip
float:round
-rw-r--r--CHANGELOG.md3
-rw-r--r--gen/src/float.erl5
-rw-r--r--gen/test/float_test.erl10
-rw-r--r--src/float.gleam2
-rw-r--r--test/float_test.gleam26
5 files changed, 43 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2f5b603..417e7e1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,7 +9,8 @@
- `str` module gains `append` function and loses `from_int`, `parse_int`,
`from_float`, `parse_float`, and `base_from_int`.
- `int` module created with `parse`, `to_string`, and `to_base_string`.
-- `float` module created with `ceiling`, `floor`, `parse`, and `to_string`.
+- `float` module created with `ceiling`, `floor`, `round`, `parse`, and
+ `to_string`.
## v0.1.1 - 2019-04-17
diff --git a/gen/src/float.erl b/gen/src/float.erl
index c290e97..f9e4064 100644
--- a/gen/src/float.erl
+++ b/gen/src/float.erl
@@ -1,7 +1,7 @@
-module(float).
-compile(no_auto_import).
--export([parse/1, to_string/1, ceiling/1, floor/1]).
+-export([parse/1, to_string/1, ceiling/1, floor/1, round/1]).
parse(A) ->
gleam__stdlib:parse_float(A).
@@ -14,3 +14,6 @@ ceiling(A) ->
floor(A) ->
math:floor(A).
+
+round(A) ->
+ erlang:round(A).
diff --git a/gen/test/float_test.erl b/gen/test/float_test.erl
index 8e9da5e..ecd210b 100644
--- a/gen/test/float_test.erl
+++ b/gen/test/float_test.erl
@@ -1,7 +1,7 @@
-module(float_test).
-compile(no_auto_import).
--export([parse_test/0, to_string_test/0, ceiling_test/0, floor_test/0]).
+-export([parse_test/0, to_string_test/0, ceiling_test/0, floor_test/0, round_test/0]).
parse_test() ->
expect:equal(float:parse(<<"1.23">>), {ok, 1.23}),
@@ -24,3 +24,11 @@ floor_test() ->
expect:equal(float:floor(8.1), 8.0),
expect:equal(float:floor(-8.1), -9.0),
expect:equal(float:floor(-8.0), -8.0).
+
+round_test() ->
+ expect:equal(float:round(8.1), 8),
+ expect:equal(float:round(8.4), 8),
+ expect:equal(float:round(8.499), 8),
+ expect:equal(float:round(8.5), 9),
+ expect:equal(float:round(-8.1), -8),
+ expect:equal(float:round(-7.5), -8).
diff --git a/src/float.gleam b/src/float.gleam
index cd1d43e..8902a82 100644
--- a/src/float.gleam
+++ b/src/float.gleam
@@ -15,3 +15,5 @@ pub fn to_string(f) {
pub external fn ceiling(Float) -> Float = "math" "ceil";
pub external fn floor(Float) -> Float = "math" "floor";
+
+pub external fn round(Float) -> Int = "erlang" "round";
diff --git a/test/float_test.gleam b/test/float_test.gleam
index 54f1463..a3bf4c1 100644
--- a/test/float_test.gleam
+++ b/test/float_test.gleam
@@ -64,3 +64,29 @@ pub fn floor_test() {
|> float:floor
|> expect:equal(_, -8.0)
}
+
+pub fn round_test() {
+ 8.1
+ |> float:round
+ |> expect:equal(_, 8)
+
+ 8.4
+ |> float:round
+ |> expect:equal(_, 8)
+
+ 8.499
+ |> float:round
+ |> expect:equal(_, 8)
+
+ 8.5
+ |> float:round
+ |> expect:equal(_, 9)
+
+ -8.1
+ |> float:round
+ |> expect:equal(_, -8)
+
+ -7.5
+ |> float:round
+ |> expect:equal(_, -8)
+}