Skip to content

Commit d2b072e

Browse files
committed
float:truncate
1 parent 8797d27 commit d2b072e

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +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`, `round`, `parse`, and
13-
`to_string`.
12+
- `float` module created with `ceiling`, `floor`, `round`, `truncate`,
13+
`parse`, and `to_string`.
1414

1515
## v0.1.1 - 2019-04-17
1616

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, round/1]).
4+
-export([parse/1, to_string/1, ceiling/1, floor/1, round/1, truncate/1]).
55

66
parse(A) ->
77
gleam__stdlib:parse_float(A).
@@ -17,3 +17,6 @@ floor(A) ->
1717

1818
round(A) ->
1919
erlang:round(A).
20+
21+
truncate(A) ->
22+
erlang:trunc(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, round_test/0]).
4+
-export([parse_test/0, to_string_test/0, ceiling_test/0, floor_test/0, round_test/0, truncate_test/0]).
55

66
parse_test() ->
77
expect:equal(float:parse(<<"1.23">>), {ok, 1.23}),
@@ -32,3 +32,11 @@ round_test() ->
3232
expect:equal(float:round(8.5), 9),
3333
expect:equal(float:round(-8.1), -8),
3434
expect:equal(float:round(-7.5), -8).
35+
36+
truncate_test() ->
37+
expect:equal(float:truncate(8.1), 8),
38+
expect:equal(float:truncate(8.4), 8),
39+
expect:equal(float:truncate(8.499), 8),
40+
expect:equal(float:truncate(8.5), 8),
41+
expect:equal(float:truncate(-8.1), -8),
42+
expect:equal(float:truncate(-7.5), -7).

src/float.gleam

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ pub external fn ceiling(Float) -> Float = "math" "ceil";
1717
pub external fn floor(Float) -> Float = "math" "floor";
1818

1919
pub external fn round(Float) -> Int = "erlang" "round";
20+
21+
pub external fn truncate(Float) -> Int = "erlang" "trunc";

test/float_test.gleam

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,29 @@ pub fn round_test() {
9090
|> float:round
9191
|> expect:equal(_, -8)
9292
}
93+
94+
pub fn truncate_test() {
95+
8.1
96+
|> float:truncate
97+
|> expect:equal(_, 8)
98+
99+
8.4
100+
|> float:truncate
101+
|> expect:equal(_, 8)
102+
103+
8.499
104+
|> float:truncate
105+
|> expect:equal(_, 8)
106+
107+
8.5
108+
|> float:truncate
109+
|> expect:equal(_, 8)
110+
111+
-8.1
112+
|> float:truncate
113+
|> expect:equal(_, -8)
114+
115+
-7.5
116+
|> float:truncate
117+
|> expect:equal(_, -7)
118+
}

0 commit comments

Comments
 (0)