Ilya
Ilya

Reputation: 1150

Elixir/Phoenix multiplication

I've been working on the project and just found this. Basically if I multiple the integer value by the some kind of decimal(float) the value I receive is not exact. For example <%= 800 * 1.1 %> in the html, returns 880.0000000000001 instead of just 880.

Any possible explanations to why it happens? And is it possible to round the number?

Edit: <%= 800 + (800 * 0.1) %> works fine

Using the reply below, I've modified code to <%= Float.round(value, 2) %>, so the value gets rounded to two decimal places

Upvotes: 1

Views: 992

Answers (1)

coderVishal
coderVishal

Reputation: 9079

To round a float use, round()

<%= 800 * 1.1 |> round %> 

Upvotes: 4

Related Questions