From d20655524f0ff6947ddc0ac084b9ffef8853f410 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sun, 18 Sep 2016 17:15:12 -0500 Subject: [PATCH] Add Round Floats To Integers as an elixir til --- README.md | 3 ++- elixir/round-floats-to-integers.md | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 elixir/round-floats-to-integers.md diff --git a/README.md b/README.md index cb79e74..df28479 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_472 TILs and counting..._ +_473 TILs and counting..._ --- @@ -99,6 +99,7 @@ _472 TILs and counting..._ - [Quitting IEx](elixir/quitting-iex.md) - [Replace Duplicates In A Keyword List](elixir/replace-duplicates-in-a-keyword-list.md) - [Reversing A List](elixir/reversing-a-list.md) +- [Round Floats To Integers](elixir/round-floats-to-integers.md) - [Run ExUnit Tests In A Deterministic Order](elixir/run-exunit-tests-in-a-deterministic-order.md) - [String Interpolation With Just About Anything](elixir/string-interpolation-with-just-about-anything.md) - [Updating Values In A Map](elixir/updating-values-in-a-map.md) diff --git a/elixir/round-floats-to-integers.md b/elixir/round-floats-to-integers.md new file mode 100644 index 0000000..0be49ef --- /dev/null +++ b/elixir/round-floats-to-integers.md @@ -0,0 +1,28 @@ +# Round Floats To Integers + +The +[`Float.round/2`](http://elixir-lang.org/docs/stable/elixir/Float.html#round/2) +function allows you to round floating point numbers. The result is another +floating point number. + +```elixir +> Float.round(1.5) +2.0 +> Float.round(1.3) +1.0 +``` + +If, instead, you always want an integer as the result of rounding, you can +use `Kernel`'s version of a rounding function: + +```elixir +> Kernel.round(1.5) +2 +> Kernel.round(1.3) +1 +``` + +The +[`Kernel.round/1`](http://elixir-lang.org/docs/stable/elixir/Kernel.html#round/1) +function accepts both integers and floats, and always returns an integer. +Additionally, it can be used in guard tests.