Skip to content

Commit 8797d27

Browse files
committed
float:round
1 parent 63d1ef2 commit 8797d27

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
- `str` module gains `append` function and loses `from_int`, `parse_int`,
1010
`from_float`, `parse_float`, and `base_from_int`.
1111
- `int` module created with `parse`, `to_string`, and `to_base_string`.
12-
- `float` module created with `ceiling`, `floor`, `parse`, and `to_string`.
12+
- `float` module created with `ceiling`, `floor`, `round`, `parse`, and
13+
`to_string`.
1314

1415
## v0.1.1 - 2019-04-17
1516

gen/src/float.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-module(float).
22
-compile(no_auto_import).
33

4-
-export([parse/1, to_string/1, ceiling/1, floor/1]).
4+
-export([parse/1, to_string/1, ceiling/1, floor/1, round/1]).
55

66
parse(A) ->
77
gleam__stdlib:parse_float(A).
@@ -14,3 +14,6 @@ ceiling(A) ->
1414

1515
floor(A) ->
1616
math:floor(A).
17+
18+
round(A) ->
19+
erlang:round(A).

gen/test/float_test.erl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-module(float_test).
22
-compile(no_auto_import).
33

4-
-export([parse_test/0, to_string_test/0, ceiling_test/0, floor_test/0]).
4+
-export([parse_test/0, to_string_test/0, ceiling_test/0, floor_test/0, round_test/0]).
55

66
parse_test() ->
77
expect:equal(float:parse(<<"1.23">>), {ok, 1.23}),
@@ -24,3 +24,11 @@ floor_test() ->
2424
expect:equal(float:floor(8.1), 8.0),
2525
expect:equal(float:floor(-8.1), -9.0),
2626
expect:equal(float:floor(-8.0), -8.0).
27+
28+
round_test() ->
29+
expect:equal(float:round(8.1), 8),
30+
expect:equal(float:round(8.4), 8),
31+
expect:equal(float:round(8.499), 8),
32+
expect:equal(float:round(8.5), 9),
33+
expect:equal(float:round(-8.1), -8),
34+
expect:equal(float:round(-7.5), -8).

src/float.gleam

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ pub fn to_string(f) {
1515
pub external fn ceiling(Float) -> Float = "math" "ceil";
1616

1717
pub external fn floor(Float) -> Float = "math" "floor";
18+
19+
pub external fn round(Float) -> Int = "erlang" "round";

test/float_test.gleam

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,29 @@ pub fn floor_test() {
6464
|> float:floor
6565
|> expect:equal(_, -8.0)
6666
}
67+
68+
pub fn round_test() {
69+
8.1
70+
|> float:round
71+
|> expect:equal(_, 8)
72+
73+
8.4
74+
|> float:round
75+
|> expect:equal(_, 8)
76+
77+
8.499
78+
|> float:round
79+
|> expect:equal(_, 8)
80+
81+
8.5
82+
|> float:round
83+
|> expect:equal(_, 9)
84+
85+
-8.1
86+
|> float:round
87+
|> expect:equal(_, -8)
88+
89+
-7.5
90+
|> float:round
91+
|> expect:equal(_, -8)
92+
}

0 commit comments

Comments
 (0)