[erlang-questions] casting float to integer
Per Hedeland
per@REDACTED
Sat May 12 09:50:11 CEST 2007
Håkan Stenholm <hokan.stenholm@REDACTED> wrote:
>
>Nitin Verma wrote:
>> Hi All,
>>
>> I am looking for a function/BIF that can help me.
>>
>> Problem: I need a float number's floored integer. That is if:
>> A = f(5/2), then A = 2 and
>> A = f(1000/501), then A = 1
>>
>> I found 'round/1' but it just rounds-off .
>see erlang.erl documentation:
>
>-----------------------------------------------------------------------
>
>*|trunc(Number) -> int()|*
But trunc is not the same as floor - though maybe Nitin actually wants
trunc, or even div (which "truncates" the same way as trunc), as
indicated by the examples. When I actually needed floor/1, I came up
with this somewhat bizarre-looking definition:
floor(V) when V >= 0 -> trunc(V);
floor(V) -> trunc(V - trunc(V) + 1) + trunc(V) - 1.
There may be a better way to do it...
--Per Hedeland
More information about the erlang-questions
mailing list